Skip to content

Commit

Permalink
fix(dereferencing): set null as the value of the property (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Oct 1, 2019
1 parent 41fdad4 commit 99679a5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 53 deletions.
28 changes: 14 additions & 14 deletions src/__tests__/__snapshots__/parse.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ exports[`parse dereferencing anchor refs insane edge case 1`] = `
Array [
Array [
Array [
undefined,
null,
Object {
"test": Array [
undefined,
null,
],
},
Object {
"abc": Object {
"a": null,
"c": undefined,
"c": null,
"foo": 2,
"x": undefined,
"x": null,
},
},
],
Object {
"test": Array [
Array [
undefined,
null,
Object {
"test": Array [
undefined,
null,
],
},
Object {
"abc": Object {
"a": null,
"c": undefined,
"c": null,
"foo": 2,
"x": undefined,
"x": null,
},
},
],
Expand All @@ -43,27 +43,27 @@ Array [
"abc": Object {
"a": null,
"c": Array [
undefined,
null,
Object {
"test": Array [
undefined,
null,
],
},
Object {
"abc": Object {
"a": null,
"c": undefined,
"c": null,
"foo": 2,
"x": undefined,
"x": null,
},
},
],
"foo": 2,
"x": Object {
"a": null,
"c": undefined,
"c": null,
"foo": 2,
"x": undefined,
"x": null,
},
},
},
Expand Down
28 changes: 14 additions & 14 deletions src/__tests__/__snapshots__/parseWithPointers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ exports[`yaml parser dereferencing anchor refs insane edge case 1`] = `
Array [
Array [
Array [
undefined,
null,
Object {
"test": Array [
undefined,
null,
],
},
Object {
"abc": Object {
"a": null,
"c": undefined,
"c": null,
"foo": 2,
"x": undefined,
"x": null,
},
},
],
Object {
"test": Array [
Array [
undefined,
null,
Object {
"test": Array [
undefined,
null,
],
},
Object {
"abc": Object {
"a": null,
"c": undefined,
"c": null,
"foo": 2,
"x": undefined,
"x": null,
},
},
],
Expand All @@ -43,27 +43,27 @@ Array [
"abc": Object {
"a": null,
"c": Array [
undefined,
null,
Object {
"test": Array [
undefined,
null,
],
},
Object {
"abc": Object {
"a": null,
"c": undefined,
"c": null,
"foo": 2,
"x": undefined,
"x": null,
},
},
],
"foo": 2,
"x": Object {
"a": null,
"c": undefined,
"c": null,
"foo": 2,
"x": undefined,
"x": null,
},
},
},
Expand Down
17 changes: 9 additions & 8 deletions src/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ to save space`,
european-cities:
austria: *austrian-cities
`);
expect(result).toEqual({
expect(result).toStrictEqual({
'austrian-cities': ['Vienna', 'Graz', 'Linz', 'Salzburg'],
'european-cities': {
austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'],
Expand All @@ -68,13 +68,13 @@ european-cities:
name: *ref
`);

expect(result).toEqual({
expect(result).toStrictEqual({
definitions: {
model: {
foo: {
name: {
foo: {
name: undefined,
name: null,
},
},
},
Expand All @@ -90,10 +90,10 @@ european-cities:
- test:
- *foo
`);
expect(result).toEqual([
expect(result).toStrictEqual([
[
{
test: [[{ test: [undefined] }]],
test: [[{ test: [null] }]],
},
],
]);
Expand All @@ -113,10 +113,11 @@ european-cities: &cities
all: *cities
`);

expect(result).toEqual({
expect(result).toStrictEqual({
'austrian-cities': ['Vienna', 'Graz', 'Linz', 'Salzburg'],
'european-cities': {
all: {
all: null,
austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'],
},
austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'],
Expand All @@ -134,10 +135,10 @@ european-cities: &cities
- *foo
`);

expect(result).toEqual({
expect(result).toStrictEqual({
a: [
{
b: [true, { c: [true, { c: undefined }, undefined] }, [{ b: [true, { c: undefined }, undefined] }]],
b: [true, { c: [true, { c: null }, null] }, [{ b: [true, { c: null }, null] }]],
},
],
});
Expand Down
11 changes: 6 additions & 5 deletions src/__tests__/parseWithPointers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ european-cities:
foo: {
name: {
foo: {
name: undefined,
name: null,
},
},
},
Expand All @@ -189,7 +189,7 @@ european-cities:
expect(result.data).toEqual([
[
{
test: [[{ test: [undefined] }]],
test: [[{ test: [null] }]],
},
],
]);
Expand All @@ -209,10 +209,11 @@ european-cities: &cities
all: *cities
`);

expect(result.data).toEqual({
expect(result.data).toStrictEqual({
'austrian-cities': ['Vienna', 'Graz', 'Linz', 'Salzburg'],
'european-cities': {
all: {
all: null,
austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'],
},
austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'],
Expand All @@ -230,10 +231,10 @@ european-cities: &cities
- *foo
`);

expect(result.data).toEqual({
expect(result.data).toStrictEqual({
a: [
{
b: [true, { c: [true, { c: undefined }, undefined] }, [{ b: [true, { c: undefined }, undefined] }]],
b: [true, { c: [true, { c: null }, null] }, [{ b: [true, { c: null }, null] }]],
},
],
});
Expand Down
23 changes: 11 additions & 12 deletions src/parseWithPointers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DiagnosticSeverity, IDiagnostic, Optional } from '@stoplight/types';
import { DiagnosticSeverity, IDiagnostic } from '@stoplight/types';
import {
determineScalarType,
load as loadAST,
Expand All @@ -15,11 +15,10 @@ import {
Kind,
ScalarType,
YAMLAnchorReference,
YAMLMap,
YAMLMapping,
YAMLNode,
YamlParserResult,
YAMLScalar,
YAMLSequence,
} from './types';
import { isObject } from './utils';

Expand Down Expand Up @@ -114,7 +113,7 @@ export const walkAST = (
return getScalarValue(node);
case Kind.ANCHOR_REF: {
if (isObject(node.value) && isCircularAnchorRef(node)) {
node.value = (dereferenceAnchor(node.value, node.referencesAnchor) as YAMLNode) || void 0;
node.value = dereferenceAnchor(node.value, node.referencesAnchor)!;
}

return node.value && walkAST(node.value, options, duplicatedMappingKeys);
Expand All @@ -140,28 +139,28 @@ const isCircularAnchorRef = (anchorRef: YAMLAnchorReference) => {
return false;
};

const dereferenceAnchor = (node: YAMLNode | null, anchorId: string): Optional<YAMLNode | YAMLNode[] | null> => {
const dereferenceAnchor = (node: YAMLNode | null, anchorId: string): YAMLNode | null => {
if (!isObject(node)) return node;
if (node.kind === Kind.ANCHOR_REF && node.referencesAnchor === anchorId) return;
if (node.kind === Kind.ANCHOR_REF && node.referencesAnchor === anchorId) return null;

switch (node.kind) {
case Kind.MAP:
return {
...node,
mappings: node.mappings.map(mapping => dereferenceAnchor(mapping, anchorId)),
} as YAMLMap;
mappings: node.mappings.map(mapping => dereferenceAnchor(mapping, anchorId) as YAMLMapping),
};
case Kind.SEQ:
return {
...node,
items: node.items.map(item => dereferenceAnchor(item, anchorId)),
} as YAMLSequence;
items: node.items.map(item => dereferenceAnchor(item, anchorId)!),
};
case Kind.MAPPING:
return { ...node, value: dereferenceAnchor(node.value, anchorId) as YAMLNode };
return { ...node, value: dereferenceAnchor(node.value, anchorId) };
case Kind.SCALAR:
return node;
case Kind.ANCHOR_REF:
if (isObject(node.value) && isCircularAnchorRef(node)) {
return;
return null;
}

return node;
Expand Down

0 comments on commit 99679a5

Please sign in to comment.