Skip to content

Commit

Permalink
test: complex case
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleh Shumov committed Dec 9, 2024
1 parent e790dec commit ae39d1e
Show file tree
Hide file tree
Showing 3 changed files with 364 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
{
"openapi": "3.0.3",
"info": {
"title": "API with Cyclic References",
"version": "1.0.0"
},
"paths": {
"/example": {
"get": {
"summary": "Retrieve data with cyclic references",
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"nodeA": {
"$ref": "#/components/schemas/NodeA"
},
"nodeC": {
"$ref": "#/components/schemas/NodeC"
},
"nodeD": {
"$ref": "#/components/schemas/NodeD"
},
"complexNode": {
"$ref": "#/components/schemas/ComplexNode"
},
"selfReferencingNode": {
"$ref": "#/components/schemas/SelfReferencingNode"
},
"deepRecursiveNode": {
"$ref": "#/components/schemas/DeepRecursiveNode"
}
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"NodeA": {
"type": "object",
"properties": {
"nodeB": {
"$ref": "#/components/schemas/NodeB"
}
}
},
"NodeB": {
"type": "object",
"properties": {
"nodeA": {
"$ref": "#/components/schemas/NodeARef"
}
}
},
"NodeC": {
"type": "object",
"properties": {
"self": {
"$ref": "#/components/schemas/NodeCRef"
}
}
},
"NodeD": {
"type": "object",
"properties": {
"nodeE": {
"$ref": "#/components/schemas/NodeE"
}
}
},
"NodeE": {
"type": "object",
"properties": {
"nodeD": {
"$ref": "#/components/schemas/NodeDRef"
}
}
},
"ComplexNode": {
"type": "object",
"properties": {
"nodeF": {
"$ref": "#/components/schemas/NodeF"
}
}
},
"NodeF": {
"type": "object",
"properties": {
"nodeG": {
"$ref": "#/components/schemas/NodeG"
}
}
},
"NodeG": {
"type": "object",
"properties": {
"nodeH": {
"$ref": "#/components/schemas/NodeH"
}
}
},
"NodeH": {
"type": "object",
"properties": {
"complexNode": {
"$ref": "#/components/schemas/ComplexNodeRef"
}
}
},
"SelfReferencingNode": {
"type": "object",
"properties": {
"node": {
"type": "object"
}
}
},
"DeepRecursiveNode": {
"type": "object",
"properties": {
"child": {
"$ref": "#/components/schemas/DeepRecursiveNodeRef"
}
}
},
"DeepRecursiveNodeRef": {
"type": "object",
"properties": {
"child": {
"type": "object"
}
}
},
"NodeARef": {
"type": "object",
"properties": {
"nodeB": {
"type": "object"
}
}
},
"NodeCRef": {
"type": "object",
"properties": {
"self": {
"type": "object"
}
}
},
"NodeDRef": {
"type": "object",
"properties": {
"nodeE": {
"type": "object"
}
}
},
"ComplexNodeRef": {
"type": "object",
"properties": {
"nodeF": {
"$ref": "#/components/schemas/NodeFRef"
}
}
},
"NodeFRef": {
"type": "object",
"properties": {
"nodeG": {
"$ref": "#/components/schemas/NodeGRef"
}
}
},
"NodeGRef": {
"type": "object",
"properties": {
"nodeH": {
"$ref": "#/components/schemas/NodeHRef"
}
}
},
"NodeHRef": {
"type": "object",
"properties": {
"complexNode": {
"type": "object"
}
}
}
}
}
}
138 changes: 138 additions & 0 deletions __tests__/__fixtures__/circular-references-oas/combined-cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"openapi": "3.0.3",
"info": {
"title": "API with Cyclic References",
"version": "1.0.0"
},
"paths": {
"/example": {
"get": {
"summary": "Retrieve data with cyclic references",
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"nodeA": {
"$ref": "#/components/schemas/NodeA"
},
"nodeC": {
"$ref": "#/components/schemas/NodeC"
},
"nodeD": {
"$ref": "#/components/schemas/NodeD"
},
"complexNode": {
"$ref": "#/components/schemas/ComplexNode"
},
"selfReferencingNode": {
"$ref": "#/components/schemas/SelfReferencingNode"
},
"deepRecursiveNode": {
"$ref": "#/components/schemas/DeepRecursiveNode"
}
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"NodeA": {
"type": "object",
"properties": {
"nodeB": {
"$ref": "#/components/schemas/NodeB"
}
}
},
"NodeB": {
"type": "object",
"properties": {
"nodeA": {
"$ref": "#/components/schemas/NodeA"
}
}
},
"NodeC": {
"type": "object",
"properties": {
"self": {
"$ref": "#/components/schemas/NodeC"
}
}
},
"NodeD": {
"type": "object",
"properties": {
"nodeE": {
"$ref": "#/components/schemas/NodeE"
}
}
},
"NodeE": {
"type": "object",
"properties": {
"nodeD": {
"$ref": "#/components/schemas/NodeD"
}
}
},
"ComplexNode": {
"type": "object",
"properties": {
"nodeF": {
"$ref": "#/components/schemas/NodeF"
}
}
},
"NodeF": {
"type": "object",
"properties": {
"nodeG": {
"$ref": "#/components/schemas/NodeG"
}
}
},
"NodeG": {
"type": "object",
"properties": {
"nodeH": {
"$ref": "#/components/schemas/NodeH"
}
}
},
"NodeH": {
"type": "object",
"properties": {
"complexNode": {
"$ref": "#/components/schemas/ComplexNode"
}
}
},
"SelfReferencingNode": {
"type": "object",
"properties": {
"node": {
"$ref": "#/components/schemas/SelfReferencingNode"
}
}
},
"DeepRecursiveNode": {
"type": "object",
"properties": {
"child": {
"$ref": "#/components/schemas/DeepRecursiveNode"
}
}
}
}
}
}
24 changes: 24 additions & 0 deletions __tests__/commands/openapi/refs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ describe('openapi refs', () => {
const expectedOutput = JSON.parse(fs.readFileSync(expectedOutputFile, 'utf8'));
expect(processedOutput).toStrictEqual(expectedOutput);
});

it('should handle a combination of recursiveness/nested circularity within a single file', async () => {
const inputFile = path.resolve('__tests__/__fixtures__/circular-references-oas/combined-cases.json');
const expectedOutputFile = path.resolve(
'__tests__/__fixtures__/circular-references-oas/combined-cases-resolved.json',
);
const defaultOutputFilePath = 'combined-cases.openapi.json';

prompts.inject([defaultOutputFilePath]);

let processedOutput;
fs.writeFileSync = vi.fn((fileName, data) => {
processedOutput = JSON.parse(data as string);
});

const result = await run([inputFile]);

expect(result).toMatch(`Your API definition has been processed and saved to ${defaultOutputFilePath}!`);

expect(fs.writeFileSync).toHaveBeenCalledWith(defaultOutputFilePath, expect.any(String));

const expectedOutput = JSON.parse(fs.readFileSync(expectedOutputFile, 'utf8'));
expect(processedOutput).toStrictEqual(expectedOutput);
});
});

describe('error handling', () => {
Expand Down

0 comments on commit ae39d1e

Please sign in to comment.