diff --git a/src/ol/format/wfs.js b/src/ol/format/wfs.js index 5ad2431144..5b0b19c0a5 100644 --- a/src/ol/format/wfs.js +++ b/src/ol/format/wfs.js @@ -399,6 +399,25 @@ ol.format.WFS.writeOgcFidFilter_ = function(node, fid, objectStack) { }; +/** + * @param {string|undefined} featurePrefix The prefix of the feature. + * @param {string} featureType The type of the feature. + * @returns {string} The value of the typeName property. + * @private + */ +ol.format.WFS.getTypeName_ = function(featurePrefix, featureType) { + featurePrefix = featurePrefix ? featurePrefix : + ol.format.WFS.FEATURE_PREFIX; + var prefix = featurePrefix + ':'; + // The featureType already contains the prefix. + if (featureType.indexOf(prefix) === 0) { + return featureType; + } else { + return prefix + featureType; + } +}; + + /** * @param {Node} node Node. * @param {ol.Feature} feature Feature. @@ -410,10 +429,9 @@ ol.format.WFS.writeDelete_ = function(node, feature, objectStack) { ol.asserts.assert(feature.getId() !== undefined, 26); // Features must have an id set var featureType = context['featureType']; var featurePrefix = context['featurePrefix']; - featurePrefix = featurePrefix ? featurePrefix : - ol.format.WFS.FEATURE_PREFIX; var featureNS = context['featureNS']; - node.setAttribute('typeName', featurePrefix + ':' + featureType); + var typeName = ol.format.WFS.getTypeName_(featurePrefix, featureType); + node.setAttribute('typeName', typeName); ol.xml.setAttributeNS(node, ol.format.WFS.XMLNS, 'xmlns:' + featurePrefix, featureNS); var fid = feature.getId(); @@ -434,10 +452,9 @@ ol.format.WFS.writeUpdate_ = function(node, feature, objectStack) { ol.asserts.assert(feature.getId() !== undefined, 27); // Features must have an id set var featureType = context['featureType']; var featurePrefix = context['featurePrefix']; - featurePrefix = featurePrefix ? featurePrefix : - ol.format.WFS.FEATURE_PREFIX; var featureNS = context['featureNS']; - node.setAttribute('typeName', featurePrefix + ':' + featureType); + var typeName = ol.format.WFS.getTypeName_(featurePrefix, featureType); + node.setAttribute('typeName', typeName); ol.xml.setAttributeNS(node, ol.format.WFS.XMLNS, 'xmlns:' + featurePrefix, featureNS); var fid = feature.getId(); @@ -538,8 +555,14 @@ ol.format.WFS.writeQuery_ = function(node, featureType, objectStack) { var featureNS = context['featureNS']; var propertyNames = context['propertyNames']; var srsName = context['srsName']; - var prefix = featurePrefix ? featurePrefix + ':' : ''; - node.setAttribute('typeName', prefix + featureType); + var typeName; + // If feature prefix is not defined, we must not use the default prefix. + if (featurePrefix) { + typeName = ol.format.WFS.getTypeName_(featurePrefix, featureType); + } else { + typeName = featureType; + } + node.setAttribute('typeName', typeName); if (srsName) { node.setAttribute('srsName', srsName); } diff --git a/test/spec/ol/format/wfs.test.js b/test/spec/ol/format/wfs.test.js index 89b76fa652..0d9fe3ebc5 100644 --- a/test/spec/ol/format/wfs.test.js +++ b/test/spec/ol/format/wfs.test.js @@ -839,6 +839,47 @@ describe('ol.format.WFS', function() { }); }); + describe('when writing out a Transaction request', function() { + var text; + before(function(done) { + afterLoadText('spec/ol/format/wfs/TransactionMulti.xml', function(xml) { + text = xml; + done(); + }); + }); + + it('do not add feature prefx twice', function() { + var format = new ol.format.WFS(); + var insertFeature = new ol.Feature({ + the_geom: new ol.geom.MultiPoint([[1, 2]]), + foo: 'bar', + nul: null + }); + insertFeature.setGeometryName('the_geom'); + var inserts = [insertFeature]; + var updateFeature = new ol.Feature({ + the_geom: new ol.geom.MultiPoint([[1, 2]]), + foo: 'bar', + // null value gets Property element with no Value + nul: null, + // undefined value means don't create a Property element + unwritten: undefined + }); + updateFeature.setId('fid.42'); + var updates = [updateFeature]; + + var deleteFeature = new ol.Feature(); + deleteFeature.setId('fid.37'); + var deletes = [deleteFeature]; + var serialized = format.writeTransaction(inserts, updates, deletes, { + featureNS: 'http://www.openplans.org/topp', + featureType: 'topp:states', + featurePrefix: 'topp' + }); + expect(serialized).to.xmleql(ol.xml.parse(text)); + }); + }); + describe('when writing out a GetFeature request', function() { var text;