Skip to content

Commit 70228ba

Browse files
authored
Merge pull request redhat-developer#222 from redhat-developer/merge-key-fix
Fixed merge key error with JSON schema
2 parents 07ca6ca + 789e241 commit 70228ba

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/languageservice/parser/jsonParser07.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,37 @@ function validate(node: ASTNode, schema: JSONSchema, validationResult: Validatio
837837
const unprocessedProperties: string[] = [];
838838
for (const propertyNode of node.properties) {
839839
const key = propertyNode.keyNode.value;
840-
seenKeys[key] = propertyNode.valueNode;
841-
unprocessedProperties.push(key);
840+
841+
//Replace the merge key with the actual values of what the node value points to in seen keys
842+
if (key === '<<' && propertyNode.valueNode) {
843+
844+
switch (propertyNode.valueNode.type) {
845+
case 'object': {
846+
propertyNode.valueNode['properties'].forEach(propASTNode => {
847+
const propKey = propASTNode.keyNode.value;
848+
seenKeys[propKey] = propASTNode.valueNode;
849+
unprocessedProperties.push(propKey);
850+
});
851+
break;
852+
}
853+
case 'array': {
854+
propertyNode.valueNode['items'].forEach(sequenceNode => {
855+
sequenceNode['properties'].forEach(propASTNode => {
856+
const seqKey = propASTNode.keyNode.value;
857+
seenKeys[seqKey] = propASTNode.valueNode;
858+
unprocessedProperties.push(seqKey);
859+
});
860+
});
861+
break;
862+
}
863+
default: {
864+
break;
865+
}
866+
}
867+
} else {
868+
seenKeys[key] = propertyNode.valueNode;
869+
unprocessedProperties.push(key);
870+
}
842871
}
843872

844873
if (Array.isArray(schema.required)) {

test/schemaValidation.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,23 @@ suite('Validation Tests', () => {
354354
});
355355
});
356356

357+
describe('Test anchors specifically against gitlab schema', function () {
358+
it('Test that anchors do not report Property << is not allowed', done => {
359+
languageService.configure({
360+
schemas: [{
361+
uri: 'http://json.schemastore.org/gitlab-ci',
362+
fileMatch: ['*.yaml', '*.yml']
363+
}],
364+
validate: true
365+
});
366+
const content = '.test-cache: &test-cache\n cache: {}\nnodejs-tests:\n <<: *test-cache\n script: test';
367+
const validator = parseSetup(content);
368+
validator.then(function (result) {
369+
assert.equal(result.length, 0);
370+
}).then(done, done);
371+
});
372+
});
373+
357374
describe('Test with custom schemas', function () {
358375
function parseSetup(content: string) {
359376
const testTextDocument = setupTextDocument(content);
@@ -412,7 +429,6 @@ suite('Validation Tests', () => {
412429
assert.equal(result[1].message, `Value is not accepted. Valid values: "ImageStreamImport", "ImageStreamLayers".`);
413430
}).then(done, done);
414431
});
415-
416432
});
417433
});
418434
});

0 commit comments

Comments
 (0)