Skip to content

Commit

Permalink
test: prepare tests for ref with different description issue
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed May 14, 2024
1 parent 5c40157 commit 42789cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
45 changes: 20 additions & 25 deletions test/refHandling-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,64 +124,59 @@ describe('refHandling option === "import"', () => {

it('Generates expected $ref schemas (with and without $id)', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'),
openApiSchema: path.resolve(fixtures, 'ref-property/specs.yaml'),
outputPath: makeTestOutputPath('refHandling-import-ref-schemas'),
definitionPathsToGenerateFrom: ['paths'],
definitionPathsToGenerateFrom: ['components.schemas'],
silent: true,
refHandling: 'import',
});

// January schema
const actualJanuarySchemaFile = await fs.readFile(
path.resolve(outputPath, 'components/schemas/January.ts'),
// Answer schema
const actualAnswerSchemaFile = await fs.readFile(
path.resolve(outputPath, 'components/schemas/Answer.ts'),
{
encoding: 'utf8',
},
);

const expectedJanuarySchemaFile = await formatTypeScript(`
import componentsSchemasAnswer from "./Answer";
const expectedAnswerSchemaFile = await formatTypeScript(`
const schema = {
description: "January description",
type: "object",
required: ["isJanuary"],
properties: {
isJanuary: componentsSchemasAnswer,
},
type: ["string", "null"],
enum: ["yes", "no", null],
} as const;
export default schema;
const with$id = { $id: "/components/schemas/January", ...schema };
const with$id = { $id: "/components/schemas/Answer", ...schema };
export { with$id };`);

expect(actualJanuarySchemaFile).toEqual(expectedJanuarySchemaFile);
expect(actualAnswerSchemaFile).toEqual(expectedAnswerSchemaFile);

// February schema
const actualFebruarySchemaFile = await fs.readFile(
path.resolve(outputPath, 'components/schemas/February.ts'),
// January schema
const actualJanuarySchemaFile = await fs.readFile(
path.resolve(outputPath, 'components/schemas/January.ts'),
{
encoding: 'utf8',
},
);

const expectedFebruarySchemaFile = await formatTypeScript(`
const expectedJanuarySchemaFile = await formatTypeScript(`
import componentsSchemasAnswer from "./Answer";
const schema = {
description: "February description",
description: "January description",
type: "object",
required: ["isFebruary"],
required: ["isJanuary"],
properties: {
isFebruary: componentsSchemasAnswer,
isJanuary: componentsSchemasAnswer,
isFebruary: componentsSchemasAnswer
},
} as const;
export default schema;
const with$id = { $id: "/components/schemas/February", ...schema };
const with$id = { $id: "/components/schemas/January", ...schema };
export { with$id };`);

expect(actualFebruarySchemaFile).toEqual(expectedFebruarySchemaFile);
expect(actualJanuarySchemaFile).toEqual(expectedJanuarySchemaFile);
});

describe('Alias definitions', () => {
Expand Down
7 changes: 4 additions & 3 deletions test/refHandling-keep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ describe('refHandling option === "keep"', () => {

it('Generates expected $ref schemas preserving $ref pointer', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'),
definitionPathsToGenerateFrom: ['paths'],
silent: true,
openApiSchema: path.resolve(fixtures, 'ref-property/specs.yaml'),
definitionPathsToGenerateFrom: ['components.schemas'],
refHandling: 'keep',
silent: true,
});

const januarySchema = await import(
Expand All @@ -103,6 +103,7 @@ describe('refHandling option === "keep"', () => {
required: ['isJanuary'],
properties: {
isJanuary: { $ref: '/components/schemas/Answer' },
isFebruary: { $ref: '/components/schemas/Answer' },
},
});

Expand Down

0 comments on commit 42789cc

Please sign in to comment.