Skip to content

Commit a14a678

Browse files
authored
fix(walker): handle $ref siblings (#18)
1 parent 3fb8ae0 commit a14a678

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/__tests__/tree.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,86 @@ describe('SchemaTree', () => {
677677
it('given empty schema, should output empty tree', () => {
678678
expect(printTree({})).toEqual('');
679679
});
680+
681+
it('should override description', () => {
682+
const schema = {
683+
type: 'object',
684+
properties: {
685+
caves: {
686+
type: 'array',
687+
items: {
688+
summary: 'Bear cave',
689+
$ref: '#/$defs/Cave',
690+
description: 'Apparently Tom likes bears',
691+
},
692+
},
693+
greatestBear: {
694+
$ref: '#/$defs/Bear',
695+
description: 'The greatest bear!',
696+
},
697+
bestBear: {
698+
$ref: '#/$defs/Bear',
699+
summary: 'The best bear!',
700+
},
701+
},
702+
$defs: {
703+
Bear: {
704+
type: 'string',
705+
summary: "Tom's favorite bear",
706+
},
707+
Cave: {
708+
type: 'string',
709+
summary: 'A cave',
710+
description: '_Everyone_ ~hates~ loves caves',
711+
},
712+
},
713+
};
714+
715+
const tree = new SchemaTree(schema, {});
716+
tree.populate();
717+
718+
expect(tree.root).toEqual(
719+
expect.objectContaining({
720+
children: [
721+
expect.objectContaining({
722+
primaryType: 'object',
723+
types: ['object'],
724+
children: [
725+
expect.objectContaining({
726+
primaryType: 'array',
727+
subpath: ['properties', 'caves'],
728+
types: ['array'],
729+
children: [
730+
expect.objectContaining({
731+
primaryType: 'string',
732+
types: ['string'],
733+
subpath: ['items'],
734+
annotations: {
735+
description: 'Apparently Tom likes bears',
736+
},
737+
}),
738+
],
739+
}),
740+
expect.objectContaining({
741+
primaryType: 'string',
742+
types: ['string'],
743+
subpath: ['properties', 'greatestBear'],
744+
annotations: {
745+
description: 'The greatest bear!',
746+
},
747+
}),
748+
expect.objectContaining({
749+
primaryType: 'string',
750+
types: ['string'],
751+
subpath: ['properties', 'bestBear'],
752+
annotations: {},
753+
}),
754+
],
755+
}),
756+
],
757+
}),
758+
);
759+
});
680760
});
681761

682762
describe('position', () => {

src/walker/walker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,14 @@ export class Walker extends EventEmitter<WalkerEmitter> {
275275
return [new ReferenceNode(fragment, '$ref is not a string'), fragment];
276276
} else if (walkingOptions.resolveRef !== null) {
277277
try {
278-
fragment = walkingOptions.resolveRef(path, fragment.$ref);
278+
let newFragment = walkingOptions.resolveRef(path, fragment.$ref);
279+
280+
if (typeof fragment.description === 'string') {
281+
newFragment = { ...newFragment };
282+
Object.assign(newFragment, { description: fragment.description });
283+
}
284+
285+
fragment = newFragment;
279286
} catch (ex) {
280287
super.emit('error', createMagicError(ex));
281288
return [new ReferenceNode(fragment, ex?.message ?? 'Unknown resolving error'), fragment];

0 commit comments

Comments
 (0)