Skip to content

Commit

Permalink
test: unprocessable files
Browse files Browse the repository at this point in the history
  • Loading branch information
admin committed Nov 29, 2024
1 parent 19875c1 commit 43f2c82
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"openapi": "3.0.0",
"info": {
"title": "Complex Cyclic Example",
"version": "1.0.0"
},
"paths": {
"/example": {
"get": {
"summary": "Example endpoint with unresolved references",
"responses": {
"200": {
"description": "Successful response"
}
}
}
}
},
"components": {
"schemas": {
"Entity": {
"type": "object",
"properties": {
"relatedEntity": {
"$ref": "#/components/schemas/RelatedEntity"
},
"Entity": {
"$ref": "#/components/schemas/EntityRef"
}
}
},
"RelatedEntity": {
"type": "object",
"properties": {
"entity": {
"$ref": "#/components/schemas/EntityRef"
},
"RelatedEntity": {
"$ref": "#/components/schemas/RelatedEntityRef"
}
}
},
"RelatedEntityRef": {
"type": "object",
"properties": {
"entity": {
"type": "object"
},
"RelatedEntity": {
"type": "object"
}
}
},
"EntityRef": {
"type": "object",
"properties": {
"relatedEntity": {
"$ref": "#/components/schemas/RelatedEntityRef"
},
"Entity": {
"type": "object"
}
}
}
}
}
}
20 changes: 17 additions & 3 deletions __tests__/commands/openapi/refs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,30 @@ describe('openapi:solving-circularity-recursiveness', () => {
expect(processedOutput).toStrictEqual(expectedOutput);
});

it('should display warning if unresolved references remain', async () => {
it('should replace circularity that cannot be processed with empty objects', async () => {
const inputFile = path.resolve(
'__tests__/__fixtures__/circular-references-oas/unresolvable-circular-references.json',
);
const expectedOutputFile = path.resolve(
'__tests__/__fixtures__/circular-references-oas/unresolvable-circular-reference-resolved.json',
);
const defaultOutputFilePath = 'unresolvable-circular-references.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).toContain('File not saved due to unresolved circular references.');
expect(result).toMatch(`Your API definition has been processed and saved to ${defaultOutputFilePath}!`);

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

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

0 comments on commit 43f2c82

Please sign in to comment.