Skip to content

Commit

Permalink
fix: use "_" as path separator (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign authored Mar 4, 2024
1 parent 7461657 commit 06b8d4f
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-laws-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-ts-json-schema": minor
---

use `_` as path separator to support Windows OS
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export default {
type: "integer",
maximum: 100,
format: "int32",
minimum: -2147483648,
},
},
required: [],
type: "object",
},
},
responses: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {
},
},
required: ["petId"],
type: "object",
},
},
responses: {
Expand Down
5 changes: 2 additions & 3 deletions src/utils/addSchemaToMetaData.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import path from 'node:path';
// @ts-expect-error no type defs for namify
import namify from 'namify';
import filenamify from 'filenamify';
import { refToPath } from '.';
import { refToPath, filenamify } from '.';
import type { SchemaMetaDataMap, SchemaMetaData, JSONSchema } from '../types';

/*
Expand All @@ -27,7 +26,7 @@ export function addSchemaToMetaData({
const { schemaRelativeDirName, schemaName, schemaRelativePath } =
refToPath(ref);
const schemaAbsoluteDirName = path.join(outputPath, schemaRelativeDirName);
const schemaFileName = filenamify(schemaName, { replacement: '|' });
const schemaFileName = filenamify(schemaName);
const schemaAbsoluteImportPath = path.join(
schemaAbsoluteDirName,
schemaFileName,
Expand Down
16 changes: 16 additions & 0 deletions src/utils/filenamify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import _filenamify from 'filenamify';

/**
* Replace "/" occurrences with "_"
* 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: '!',
},
);
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { replaceInlinedRefsWithStringPlaceholder } from './makeTsJsonSchema/repl
export { replacePlaceholdersWithImportedSchemas } from './makeTsJsonSchema/replacePlaceholdersWithImportedSchemas';
export { addSchemaToMetaData } from './addSchemaToMetaData';
export { isObject } from './isObject';
export { filenamify } from './filenamify';

export { clearFolder } from './clearFolder';
export { makeRelativePath } from './makeRelativePath';
Expand Down
4 changes: 2 additions & 2 deletions src/utils/pathToRef.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path';
import filenamify from 'filenamify';
import { filenamify } from './';

/**
* Generate a local OpenAPI ref from a relative path and a schema name
Expand All @@ -15,7 +15,7 @@ export function pathToRef({
'#/' +
path.join(
schemaRelativeDirName.replaceAll('.', '/'),
filenamify(schemaName, { replacement: '|' }),
filenamify(schemaName),
)
);
}
2 changes: 1 addition & 1 deletion test/dereferencing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Dereferencing', () => {
});

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

expect(
Expand Down
2 changes: 1 addition & 1 deletion 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({
description: 'January description',
Expand Down
2 changes: 1 addition & 1 deletion test/paths-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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({
Expand Down
2 changes: 1 addition & 1 deletion test/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('OpenAPI paths', () => {
});

const pathSchema = await import(
path.resolve(outputPath, 'paths/users|{id}')
path.resolve(outputPath, 'paths/users_{id}')
);

const componentsSchemasUser = {
Expand Down
4 changes: 2 additions & 2 deletions test/refHandling-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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({
Expand Down Expand Up @@ -79,7 +79,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 Down
4 changes: 2 additions & 2 deletions test/refHandling-keep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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({
Expand Down Expand Up @@ -45,7 +45,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 Down
2 changes: 1 addition & 1 deletion test/schemaPatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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

0 comments on commit 06b8d4f

Please sign in to comment.