Skip to content

Commit

Permalink
test: generate schemas in different paths (#78)
Browse files Browse the repository at this point in the history
* test: import fixture path

* test: remove importFresh utility
  • Loading branch information
toomuchdesign authored Sep 18, 2023
1 parent 9e0a222 commit 3798318
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 129 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type MyModel = FromSchema<typeof myModelSchema>;

Generated JSON schema path names get escaped in order to be valid file system names.

Circular `$ref`s can be technically resolved with "import" `refHandling` option. But TS will stop the type recursion and type the schema as `any`. See [relevant tests](https://github.com/toomuchdesign/openapi-ts-json-schema/blob/master/test/circularReference.test.ts).
Circular `$ref`s can be technically resolved with "import" `refHandling` option. But TS will stop the type recursion and type the schema as `any` (error `ts(7022)`). See [relevant tests](https://github.com/toomuchdesign/openapi-ts-json-schema/blob/master/test/circularReference.test.ts).

Take a look at the [Developer's notes](./docs/developer-notes.md) for a few more in-depth explanations.

Expand Down
19 changes: 7 additions & 12 deletions test/circularReference.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import path from 'path';
import { describe, it, expect } from 'vitest';
import { importFresh } from './test-utils';
import { fixtures, makeTestOutputPath } from './test-utils';
import { openapiToTsJsonSchema } from '../src';

const fixtures = path.resolve(__dirname, 'fixtures');

describe('Circular reference', () => {
it('Works', async () => {
it("Doesn't break", async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'circular-reference/specs.yaml'),
outputPath: makeTestOutputPath('circular'),
definitionPathsToGenerateFrom: ['components.schemas'],
refHandling: 'import',
silent: true,
});

const januarySchema = await importFresh(
path.resolve(outputPath, 'components/schemas/January'),
const januarySchema = await import(
path.resolve(outputPath, 'components/schemas/January')
);

expect(januarySchema.default).toEqual({
Expand All @@ -26,12 +25,8 @@ describe('Circular reference', () => {
description: 'February description',
type: 'object',
properties: {
previousMonth: {
description: 'January description',
type: 'object',
// @NOTE JS engine seems to stop recursion
properties: { nextMonth: undefined },
},
// Node stops recursion
previousMonth: undefined,
},
},
},
Expand Down
16 changes: 8 additions & 8 deletions test/dereferencing.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import path from 'path';
import { describe, it, expect } from 'vitest';
import { importFresh } from './test-utils';
import { fixtures, makeTestOutputPath } from './test-utils';
import { openapiToTsJsonSchema } from '../src';

const fixtures = path.resolve(__dirname, 'fixtures');

describe('Deferencing', () => {
describe('Dereferencing', () => {
it('Dereferences and transforms even from paths not marked for generation', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'),
outputPath: makeTestOutputPath('dereferencing'),
definitionPathsToGenerateFrom: ['components.months'],
silent: true,
});

const januarySchema = await importFresh(
path.resolve(outputPath, 'components/months/January'),
const januarySchema = await import(
path.resolve(outputPath, 'components/months/January')
);

expect(januarySchema.default).toEqual({
Expand All @@ -30,12 +29,13 @@ describe('Deferencing', () => {
it('Transforms deeply nested schemas', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'),
outputPath: makeTestOutputPath('dereferencing'),
definitionPathsToGenerateFrom: ['paths'],
silent: true,
});

const pathsSchema = await importFresh(
path.resolve(outputPath, 'paths/v1|path-1'),
const pathsSchema = await import(
path.resolve(outputPath, 'paths/v1|path-1')
);

expect(
Expand Down
13 changes: 6 additions & 7 deletions test/externalRefs.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import path from 'path';
import { describe, it, expect } from 'vitest';
import { importFresh } from './test-utils';
import { fixtures, makeTestOutputPath } from './test-utils';
import { openapiToTsJsonSchema } from '../src';

const fixtures = path.resolve(__dirname, 'fixtures');

describe('External $ref', () => {
it('Resolve external refs', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'external-ref/specs.yaml'),
outputPath: makeTestOutputPath('external-refs'),
definitionPathsToGenerateFrom: ['components.schemas'],
silent: true,
});

const externalDefinitionSchema = await importFresh(
path.resolve(outputPath, 'components/schemas/ExternalDefinition'),
const externalDefinitionSchema = await import(
path.resolve(outputPath, 'components/schemas/ExternalDefinition')
);

expect(externalDefinitionSchema.default).toEqual({
Expand All @@ -23,11 +22,11 @@ describe('External $ref', () => {
enum: ['yes', 'no', null],
});

const localDefinitionReferencingExternalSchema = await importFresh(
const localDefinitionReferencingExternalSchema = await import(
path.resolve(
outputPath,
'components/schemas/LocalDefinitionReferencingExternal',
),
)
);

expect(localDefinitionReferencingExternalSchema.default).toEqual({
Expand Down
13 changes: 6 additions & 7 deletions test/idExport.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import path from 'path';
import { describe, it, expect } from 'vitest';
import { openapiToTsJsonSchema } from '../src';
import { importFresh } from './test-utils';

const fixtures = path.resolve(__dirname, 'fixtures');
import { fixtures, makeTestOutputPath } from './test-utils';

describe('$id export', async () => {
it('exposes schema id as $id named export', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'),
outputPath: makeTestOutputPath('id-export'),
definitionPathsToGenerateFrom: ['components.months'],
silent: true,
});

const januarySchema = await importFresh(
path.resolve(outputPath, 'components/months/January'),
const januarySchema = await import(
path.resolve(outputPath, 'components/months/January')
);
const februarySchema = await importFresh(
path.resolve(outputPath, 'components/months/February'),
const februarySchema = await import(
path.resolve(outputPath, 'components/months/February')
);

expect(januarySchema['$id']).toBe('#/components/months/January');
Expand Down
30 changes: 15 additions & 15 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import path from 'path';
import { existsSync } from 'fs';
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { importFresh } from './test-utils';
import { fixtures, makeTestOutputPath } from './test-utils';
import { openapiToTsJsonSchema } from '../src';

const fixtures = path.resolve(__dirname, 'fixtures');

describe('openapiToTsJsonSchema', () => {
it('Generates expected JSON schemas', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'),
outputPath: makeTestOutputPath('index'),
definitionPathsToGenerateFrom: ['paths', 'components.months'],
silent: true,
});

expect(outputPath).toBe(
path.resolve(fixtures, 'complex/schemas-autogenerated'),
);

const januarySchema = await importFresh(
path.resolve(outputPath, 'components/months/January'),
const januarySchema = await import(
path.resolve(outputPath, 'components/months/January')
);
const februarySchema = await importFresh(
path.resolve(outputPath, 'components/months/February'),
const februarySchema = await import(
path.resolve(outputPath, 'components/months/February')
);

// definition paths get escaped
const path1 = await importFresh(
path.resolve(outputPath, 'paths/v1|path-1'),
);
const path1 = await import(path.resolve(outputPath, 'paths/v1|path-1'));

expect(januarySchema.default).toEqual({
description: 'January description',
Expand Down Expand Up @@ -90,8 +83,11 @@ describe('openapiToTsJsonSchema', () => {
});

it('deletes previously generated schemas', async () => {
const { outputPath } = await openapiToTsJsonSchema({
const outputPath = makeTestOutputPath('index');

await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'),
outputPath,
definitionPathsToGenerateFrom: ['components.schemas'],
silent: true,
});
Expand All @@ -106,6 +102,7 @@ describe('openapiToTsJsonSchema', () => {

await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'),
outputPath,
definitionPathsToGenerateFrom: ['components.months'],
silent: true,
});
Expand All @@ -116,6 +113,7 @@ describe('openapiToTsJsonSchema', () => {
await expect(() =>
openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'does-not-exist.yaml'),
outputPath: makeTestOutputPath('index'),
definitionPathsToGenerateFrom: ['components'],
silent: true,
}),
Expand All @@ -134,6 +132,7 @@ describe('openapiToTsJsonSchema', () => {
it('logs expected message', async () => {
await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'),
outputPath: makeTestOutputPath('index'),
definitionPathsToGenerateFrom: [],
});

Expand All @@ -148,6 +147,7 @@ describe('openapiToTsJsonSchema', () => {
await expect(
openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'),
outputPath: makeTestOutputPath('index'),
definitionPathsToGenerateFrom: ['paths', '/components.schema'],
}),
).rejects.toThrow(
Expand Down
9 changes: 4 additions & 5 deletions test/jsonOpenAPI.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import path from 'path';
import { describe, it, expect } from 'vitest';
import { importFresh } from './test-utils';
import { fixtures, makeTestOutputPath } from './test-utils';
import { openapiToTsJsonSchema } from '../src';

const fixtures = path.resolve(__dirname, 'fixtures');

describe('JSON OpenAPI input', async () => {
it('generates expected schemas', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'json/specs.json'),
outputPath: makeTestOutputPath('json'),
definitionPathsToGenerateFrom: ['components.months'],
silent: true,
});

const januarySchema = await importFresh(
path.resolve(outputPath, 'components/months/January'),
const januarySchema = await import(
path.resolve(outputPath, 'components/months/January')
);

expect(januarySchema.default).toEqual({
Expand Down
4 changes: 2 additions & 2 deletions test/metaData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import path from 'path';
import fs from 'fs';
import { describe, it, expect } from 'vitest';
import { openapiToTsJsonSchema } from '../src';

const fixtures = path.resolve(__dirname, 'fixtures');
import { fixtures, makeTestOutputPath } from './test-utils';

describe('Returned "metaData"', async () => {
it('returns expected data', async () => {
const { metaData } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'),
outputPath: makeTestOutputPath('meta-data'),
definitionPathsToGenerateFrom: ['components.months'],
refHandling: 'import',
silent: true,
Expand Down
28 changes: 25 additions & 3 deletions test/outputPath.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'path';
import fs from 'fs';
import { describe, it, expect } from 'vitest';
import { fixtures } from './test-utils';
import { openapiToTsJsonSchema } from '../src';

const fixtures = path.resolve(__dirname, 'fixtures');

describe('"outputPath" option', async () => {
describe('"outputPath" option', () => {
it('saves generated schemas to provided path', async () => {
const customOutputPath = path.resolve(
fixtures,
Expand All @@ -29,4 +28,27 @@ describe('"outputPath" option', async () => {
);
expect(fs.existsSync(expectedGeneratedSchemaPath)).toBe(true);
});

describe('no value provided', () => {
it('saves generated schemas in a "schemas-autogenerated" folder next to open api schema', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'),
definitionPathsToGenerateFrom: ['components.schemas'],
silent: true,
});

const expectedOutputPath = path.resolve(
fixtures,
'mini-referenced/schemas-autogenerated',
);
expect(outputPath).toBe(expectedOutputPath);

const expectedGeneratedSchemaPath = path.resolve(
expectedOutputPath,
'components/schemas',
'Answer.ts',
);
expect(fs.existsSync(expectedGeneratedSchemaPath)).toBe(true);
});
});
});
9 changes: 4 additions & 5 deletions test/parameters.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import path from 'path';
import { describe, it, expect } from 'vitest';
import { importFresh } from './test-utils';
import { fixtures, makeTestOutputPath } from './test-utils';
import { openapiToTsJsonSchema } from '../src';

const fixtures = path.resolve(__dirname, 'fixtures');

describe('OpenAPI parameters', () => {
it('Transforms parameters array into a JSON schema record', async () => {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: path.resolve(fixtures, 'parameters/specs.yaml'),
outputPath: makeTestOutputPath('parameters'),
definitionPathsToGenerateFrom: ['paths'],
silent: true,
});

const pathSchema = await importFresh(
path.resolve(outputPath, 'paths/v1|path-1'),
const pathSchema = await import(
path.resolve(outputPath, 'paths/v1|path-1')
);

expect(pathSchema.default).toEqual({
Expand Down
Loading

0 comments on commit 3798318

Please sign in to comment.