Skip to content

Commit

Permalink
cherry pick #10060
Browse files Browse the repository at this point in the history
* Correctly migrate resolvedImage type fields from legacy functions (#10060)

Co-authored-by: Saman Bemel-Benrud <[email protected]>
  • Loading branch information
Ryan Hamley and samanpwbb authored Nov 6, 2020
1 parent 520a4ca commit f6dac27
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/style-spec/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 13.17.1

### 🐛 Bug fixes
* Fix a bug that resulted in some legacy functions being converted during style migration to invalid expressions on properties with the `resolvedImage`type. [#10060](https://github.com/mapbox/mapbox-gl-js/pull/10060)

## 13.17.0

### ✨ Features and improvements
Expand Down
20 changes: 18 additions & 2 deletions src/style-spec/function/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ function coalesce(a, b) {
if (b !== undefined) return b;
}

function getFallback(parameters, propertySpec) {
const defaultValue = convertLiteral(coalesce(parameters.default, propertySpec.default));

/*
* Some fields with type: resolvedImage have an undefined default.
* Because undefined is an invalid value for resolvedImage, set fallback to
* an empty string instead of undefined to ensure output
* passes validation.
*/
if (defaultValue === undefined && propertySpec.type === 'resolvedImage') {
return '';
}
return defaultValue;
}

function convertPropertyFunction(parameters, propertySpec, stops) {
const type = getFunctionType(parameters, propertySpec);
const get = ['get', parameters.property];
Expand All @@ -130,14 +145,15 @@ function convertPropertyFunction(parameters, propertySpec, stops) {
for (const stop of stops) {
expression.push(['==', get, stop[0]], stop[1]);
}
expression.push(convertLiteral(coalesce(parameters.default, propertySpec.default)));

expression.push(getFallback(parameters, propertySpec));
return expression;
} else if (type === 'categorical') {
const expression = ['match', get];
for (const stop of stops) {
appendStopPair(expression, stop[0], stop[1], false);
}
expression.push(convertLiteral(coalesce(parameters.default, propertySpec.default)));
expression.push(getFallback(parameters, propertySpec));
return expression;
} else if (type === 'interval') {
const expression = ['step', ['number', get]];
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mapbox/mapbox-gl-style-spec",
"description": "a specification for mapbox gl styles",
"version": "13.17.0",
"version": "13.17.1",
"author": "Mapbox",
"keywords": [
"mapbox",
Expand Down
35 changes: 35 additions & 0 deletions test/unit/style-spec/migrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,41 @@ test('converts stop functions to expressions', (t) => {
t.end();
});

test('converts categorical function on resolvedImage type to valid expression', (t) => {
const migrated = migrate({
version: 8,
sources: {
streets: {
url: 'mapbox://mapbox.streets',
type: 'vector'
}
},
layers: [{
id: '1',
source: 'streets',
'source-layer': 'labels',
type: 'symbol',
layout: {
'icon-image': {
base: 1,
type: 'categorical',
property: 'type',
stops: [['park', 'some-icon']]
}
}
}]
}, spec.latest.$version);
t.deepEqual(migrated.layers[0].layout['icon-image'], [
"match",
["get", "type" ],
"park",
"some-icon",
""
]);
t.deepEqual(validate.parsed(migrated, v8), []);
t.end();
});

glob.sync(`${__dirname}/fixture/v7-migrate/*.input.json`).forEach((file) => {
test(path.basename(file), (t) => {
const outputfile = file.replace('.input', '.output');
Expand Down

0 comments on commit f6dac27

Please sign in to comment.