Skip to content

Commit

Permalink
fix: schema ID generation to preserve leading "/"s (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed May 10, 2024
1 parent caed72b commit 3a77c85
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-worms-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-ts-json-schema": patch
---

Fix "/" api paths being stored out of paths folder
5 changes: 5 additions & 0 deletions .changeset/quiet-dolphins-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-ts-json-schema": minor
---

Leading "/" in schema filenames not removed anymore. Path schemas filanames handled as: "/my/path" --> "\_my_path".
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const schema = {
$id: "/paths/pets",
$id: "/paths/_pets",
get: {
summary: "List all pets",
operationId: "listPets",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const schema = {
$id: "/paths/pets_{petId}",
$id: "/paths/_pets_{petId}",
get: {
summary: "Info for a specific pet",
operationId: "showPetById",
Expand Down
10 changes: 3 additions & 7 deletions src/utils/filenamify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import _filenamify from 'filenamify';
* and any other file path unsafe character with "!"
*/

const TRAILING_SLASH_REGEX = /^\//;
export function filenamify(name: string): string {
return _filenamify(
name.replace(TRAILING_SLASH_REGEX, '').replaceAll('/', '_'),
{
replacement: '!',
},
);
return _filenamify(name.replaceAll('/', '_'), {
replacement: '!',
});
}
2 changes: 1 addition & 1 deletion test/dereferencing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Dereferencing', () => {
});

const pathsSchema = await import(
path.resolve(outputPath, 'paths/v1_path-1')
path.resolve(outputPath, 'paths/_v1_path-1')
);

expect(
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/paths/specs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ info:
version: 1.0.0

paths:
/:
get:
responses:
'200':
content:
application/json:
schema:
nullable: true
type: string

/users/{id}:
get:
tags:
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('openapiToTsJsonSchema', () => {
);

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

expect(januarySchema.default).toEqual({
$id: '/components/schemas/January',
Expand All @@ -44,7 +44,7 @@ describe('openapiToTsJsonSchema', () => {
});

expect(path1.default).toEqual({
$id: '/paths/v1_path-1',
$id: '/paths/_v1_path-1',
get: {
responses: {
'200': {
Expand Down
6 changes: 4 additions & 2 deletions test/openAPI-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ describe('OpenAPI v2', async () => {
},
});

const petsPathSchema = await import(path.resolve(outputPath, 'paths/pets'));
const petsPathSchema = await import(
path.resolve(outputPath, 'paths/_pets')
);
expect(petsPathSchema.default).toEqual({
$id: '/paths/pets',
$id: '/paths/_pets',
get: {
description:
'Returns all pets from the system that the user has access to',
Expand Down
4 changes: 2 additions & 2 deletions test/paths-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ describe('OpenAPI paths parameters', () => {
});

const pathSchema = await import(
path.resolve(outputPath, 'paths/v1_path-1')
path.resolve(outputPath, 'paths/_v1_path-1')
);

expect(pathSchema.default).toEqual({
$id: '/paths/v1_path-1',
$id: '/paths/_v1_path-1',
parameters: {
headers: {
type: 'object',
Expand Down
28 changes: 23 additions & 5 deletions test/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,33 @@ import { openapiToTsJsonSchema } from '../src';

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

const pathSchema = await import(
path.resolve(outputPath, 'paths/users_{id}')
const rootPathSchema = await import(path.resolve(outputPath, 'paths/_'));
expect(rootPathSchema.default).toEqual({
$id: '/paths/_',
get: {
responses: {
'200': {
content: {
'application/json': {
schema: {
type: ['string', 'null'],
},
},
},
},
},
},
});

const usersPathSchema = await import(
path.resolve(outputPath, 'paths/_users_{id}')
);

const componentsSchemasUser = {
Expand All @@ -29,8 +47,8 @@ describe('OpenAPI paths', () => {
required: ['id', 'name'],
};

expect(pathSchema.default).toEqual({
$id: '/paths/users_{id}',
expect(usersPathSchema.default).toEqual({
$id: '/paths/_users_{id}',
get: {
tags: ['Users'],
summary: 'Gets a user by ID.',
Expand Down
8 changes: 4 additions & 4 deletions test/refHandling-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ describe('refHandling option === "import"', () => {
refHandling: 'import',
});

const path1 = await import(path.resolve(outputPath, 'paths/v1_path-1'));
const path1 = await import(path.resolve(outputPath, 'paths/_v1_path-1'));

// Expectations against parsed root schema
expect(path1.default).toEqual({
$id: '/paths/v1_path-1',
$id: '/paths/_v1_path-1',
get: {
responses: {
'200': {
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('refHandling option === "import"', () => {

// Expectations against actual root schema file (make sure it actually imports refs :))
const actualPath1File = await fs.readFile(
path.resolve(outputPath, 'paths/v1_path-1.ts'),
path.resolve(outputPath, 'paths/_v1_path-1.ts'),
{
encoding: 'utf8',
},
Expand All @@ -91,7 +91,7 @@ describe('refHandling option === "import"', () => {
import { without$id as componentsSchemasJanuary } from "./../components/schemas/January";
const schema = {
$id: "/paths/v1_path-1",
$id: "/paths/_v1_path-1",
get: {
responses: {
"200": {
Expand Down
8 changes: 4 additions & 4 deletions test/refHandling-keep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ describe('refHandling option === "keep"', () => {
refHandling: 'keep',
});

const path1 = await import(path.resolve(outputPath, 'paths/v1_path-1'));
const path1 = await import(path.resolve(outputPath, 'paths/_v1_path-1'));

// Expectations against parsed root schema
expect(path1.default).toEqual({
$id: '/paths/v1_path-1',
$id: '/paths/_v1_path-1',
get: {
responses: {
'200': {
Expand All @@ -46,7 +46,7 @@ describe('refHandling option === "keep"', () => {

// Expectations against actual root schema file
const actualPath1File = await fs.readFile(
path.resolve(outputPath, 'paths/v1_path-1.ts'),
path.resolve(outputPath, 'paths/_v1_path-1.ts'),
{
encoding: 'utf8',
},
Expand All @@ -55,7 +55,7 @@ describe('refHandling option === "keep"', () => {
// Ensure "as const" is present
const expectedPath1File = await formatTypeScript(`
const schema = {
$id: '/paths/v1_path-1',
$id: '/paths/_v1_path-1',
get: {
responses: {
"200": {
Expand Down
2 changes: 1 addition & 1 deletion test/schemaPatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('"schemaPatcher" option', () => {

// Testing deep nested props being patched, too
const pathSchema = await import(
path.resolve(outputPath, 'paths/v1_path-1')
path.resolve(outputPath, 'paths/_v1_path-1')
);

expect(
Expand Down
21 changes: 21 additions & 0 deletions test/unit/makeId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ describe('makeId', () => {
schemaName: 'Foo',
expected: '/components/schemas/Foo',
},
{
schemaRelativeDirName: 'components.schemas',
schemaName: 'Foo/bar',
expected: '/components/schemas/Foo_bar',
},
{
schemaRelativeDirName: 'components/schemas',
schemaName: 'Foo/bar',
expected: '/components/schemas/Foo_bar',
},
{
schemaRelativeDirName: 'paths',
schemaName: '/users',
expected: '/paths/_users',
},
{
schemaRelativeDirName: 'paths',
schemaName: '/',
expected: '/paths/_',
},

// Windows path separators
{
schemaRelativeDirName: 'components\\schemas',
Expand Down

0 comments on commit 3a77c85

Please sign in to comment.