Skip to content

Commit 9675fbb

Browse files
author
Andy Hite
committed
feat: pascalcase types and schemas
1 parent c8cf2ec commit 9675fbb

File tree

10 files changed

+72
-40
lines changed

10 files changed

+72
-40
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "openapi-typescript-codegen",
2+
"name": "@shipengine/openapi-typescript-codegen",
33
"version": "0.23.0",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
@@ -120,7 +120,7 @@
120120
"typescript": "4.8.4",
121121
"zone.js": "0.11.8"
122122
},
123-
"overrides" : {
123+
"overrides": {
124124
"rollup": "3.2.3"
125125
}
126126
}

src/openApi/v2/parser/escapeName.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ describe('escapeName', () => {
44
it('should escape', () => {
55
expect(escapeName('')).toEqual("''");
66
expect(escapeName('fooBar')).toEqual('fooBar');
7-
expect(escapeName('Foo Bar')).toEqual(`'Foo Bar'`);
8-
expect(escapeName('foo bar')).toEqual(`'foo bar'`);
9-
expect(escapeName('foo-bar')).toEqual(`'foo-bar'`);
7+
expect(escapeName('Foo Bar')).toEqual(`fooBar`);
8+
expect(escapeName('foo bar')).toEqual(`fooBar`);
9+
expect(escapeName('foo-bar')).toEqual(`fooBar`);
1010
expect(escapeName('foo.bar')).toEqual(`'foo.bar'`);
11-
expect(escapeName('foo_bar')).toEqual('foo_bar');
12-
expect(escapeName('123foo.bar')).toEqual(`'123foo.bar'`);
11+
expect(escapeName('foo_bar')).toEqual('fooBar');
12+
expect(escapeName('123foo.bar')).toEqual(`'123Foo.bar'`);
1313
expect(escapeName('@foo.bar')).toEqual(`'@foo.bar'`);
1414
expect(escapeName('$foo.bar')).toEqual(`'$foo.bar'`);
1515
expect(escapeName('_foo.bar')).toEqual(`'_foo.bar'`);
16-
expect(escapeName('123foobar')).toEqual(`'123foobar'`);
16+
expect(escapeName('123foobar')).toEqual(`'123Foobar'`);
1717
expect(escapeName('@foobar')).toEqual(`'@foobar'`);
1818
expect(escapeName('$foobar')).toEqual('$foobar');
1919
expect(escapeName('_foobar')).toEqual('_foobar');

src/openApi/v2/parser/escapeName.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import camelCase from 'camelcase';
2+
13
export const escapeName = (value: string): string => {
4+
value = value
5+
.split('.')
6+
.map(part => {
7+
const prefix = part[0] === '_' ? '_' : '';
8+
return `${prefix}${camelCase(part)}`;
9+
})
10+
.join('.');
11+
212
if (value || value === '') {
313
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
414
if (!validName) {
5-
return `'${value}'`;
15+
value = `'${value}'`;
616
}
717
}
18+
819
return value;
920
};

src/openApi/v2/parser/getModel.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import camelCase from 'camelcase';
2+
13
import type { Model } from '../../../client/interfaces/Model';
24
import { getPattern } from '../../../utils/getPattern';
35
import type { OpenApi } from '../interfaces/OpenApi';
@@ -14,8 +16,10 @@ export const getModel = (
1416
isDefinition: boolean = false,
1517
name: string = ''
1618
): Model => {
19+
const prefix = name[0] === '_' ? '_' : '';
20+
1721
const model: Model = {
18-
name,
22+
name: `${prefix}${camelCase(name, { pascalCase: true })}`,
1923
export: 'interface',
2024
type: 'any',
2125
base: 'any',

src/openApi/v2/parser/getModelComposition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Model } from '../../../client/interfaces/Model';
22
import type { ModelComposition } from '../../../client/interfaces/ModelComposition';
33
import type { OpenApi } from '../interfaces/OpenApi';
44
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
5+
import { escapeName } from './escapeName';
56
import type { getModel } from './getModel';
67
import { getModelProperties } from './getModelProperties';
78
import { getRequiredPropertiesFromComposition } from './getRequiredPropertiesFromComposition';
@@ -43,7 +44,7 @@ export const getModelComposition = (
4344
if (definition.required) {
4445
const requiredProperties = getRequiredPropertiesFromComposition(
4546
openApi,
46-
definition.required,
47+
definition.required.map(required => escapeName(required)),
4748
definitions,
4849
getModel
4950
);

src/openApi/v3/parser/escapeName.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ describe('escapeName', () => {
44
it('should escape', () => {
55
expect(escapeName('')).toEqual("''");
66
expect(escapeName('fooBar')).toEqual('fooBar');
7-
expect(escapeName('Foo Bar')).toEqual(`'Foo Bar'`);
8-
expect(escapeName('foo bar')).toEqual(`'foo bar'`);
9-
expect(escapeName('foo-bar')).toEqual(`'foo-bar'`);
7+
expect(escapeName('Foo Bar')).toEqual(`fooBar`);
8+
expect(escapeName('foo bar')).toEqual(`fooBar`);
9+
expect(escapeName('foo-bar')).toEqual(`fooBar`);
1010
expect(escapeName('foo.bar')).toEqual(`'foo.bar'`);
11-
expect(escapeName('foo_bar')).toEqual('foo_bar');
12-
expect(escapeName('123foo.bar')).toEqual(`'123foo.bar'`);
11+
expect(escapeName('foo_bar')).toEqual('fooBar');
12+
expect(escapeName('123foo.bar')).toEqual(`'123Foo.bar'`);
1313
expect(escapeName('@foo.bar')).toEqual(`'@foo.bar'`);
1414
expect(escapeName('$foo.bar')).toEqual(`'$foo.bar'`);
1515
expect(escapeName('_foo.bar')).toEqual(`'_foo.bar'`);
16-
expect(escapeName('123foobar')).toEqual(`'123foobar'`);
16+
expect(escapeName('123foobar')).toEqual(`'123Foobar'`);
1717
expect(escapeName('@foobar')).toEqual(`'@foobar'`);
1818
expect(escapeName('$foobar')).toEqual('$foobar');
1919
expect(escapeName('_foobar')).toEqual('_foobar');

src/openApi/v3/parser/escapeName.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import camelCase from 'camelcase';
2+
13
export const escapeName = (value: string): string => {
4+
value = value
5+
.split('.')
6+
.map(part => {
7+
const prefix = part[0] === '_' ? '_' : '';
8+
return `${prefix}${camelCase(part)}`;
9+
})
10+
.join('.');
11+
212
if (value || value === '') {
313
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
414
if (!validName) {
5-
return `'${value}'`;
15+
value = `'${value}'`;
616
}
717
}
18+
819
return value;
920
};

src/openApi/v3/parser/getModel.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import camelCase from 'camelcase';
2+
13
import type { Model } from '../../../client/interfaces/Model';
24
import { getPattern } from '../../../utils/getPattern';
35
import type { OpenApi } from '../interfaces/OpenApi';
@@ -15,8 +17,10 @@ export const getModel = (
1517
isDefinition: boolean = false,
1618
name: string = ''
1719
): Model => {
20+
const prefix = name[0] === '_' ? '_' : '';
21+
1822
const model: Model = {
19-
name,
23+
name: `${prefix}${camelCase(name, { pascalCase: true })}`,
2024
export: 'interface',
2125
type: 'any',
2226
base: 'any',

src/openApi/v3/parser/getModelComposition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Model } from '../../../client/interfaces/Model';
22
import type { ModelComposition } from '../../../client/interfaces/ModelComposition';
33
import type { OpenApi } from '../interfaces/OpenApi';
44
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
5+
import { escapeName } from './escapeName';
56
import type { getModel } from './getModel';
67
import { getModelProperties } from './getModelProperties';
78
import { getRequiredPropertiesFromComposition } from './getRequiredPropertiesFromComposition';
@@ -44,7 +45,7 @@ export const getModelComposition = (
4445
if (definition.required) {
4546
const requiredProperties = getRequiredPropertiesFromComposition(
4647
openApi,
47-
definition.required,
48+
definition.required.map(required => escapeName(required)),
4849
definitions,
4950
getModel
5051
);

test/__snapshots__/index.spec.ts.snap

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
549549
export { OpenAPI } from './core/OpenAPI';
550550
export type { OpenAPIConfig } from './core/OpenAPI';
551551

552-
export type { _default as _defaultDto } from './models/_default';
552+
export type { _Default as _DefaultDto } from './models/_Default';
553553
export type { ArrayWithArray as ArrayWithArrayDto } from './models/ArrayWithArray';
554554
export type { ArrayWithBooleans as ArrayWithBooleansDto } from './models/ArrayWithBooleans';
555555
export type { ArrayWithNumbers as ArrayWithNumbersDto } from './models/ArrayWithNumbers';
@@ -598,7 +598,7 @@ export type { SimpleReference as SimpleReferenceDto } from './models/SimpleRefer
598598
export type { SimpleString as SimpleStringDto } from './models/SimpleString';
599599
export type { SimpleStringWithPattern as SimpleStringWithPatternDto } from './models/SimpleStringWithPattern';
600600

601-
export { $_default } from './schemas/$_default';
601+
export { $_Default } from './schemas/$_Default';
602602
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
603603
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
604604
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';
@@ -666,12 +666,12 @@ export { TypesService } from './services/TypesService';
666666
"
667667
`;
668668

669-
exports[`v2 should generate: ./test/generated/v2/models/_default.ts 1`] = `
669+
exports[`v2 should generate: ./test/generated/v2/models/_Default.ts 1`] = `
670670
"/* istanbul ignore file */
671671
/* tslint:disable */
672672
/* eslint-disable */
673673

674-
export type _default = {
674+
export type _Default = {
675675
name?: string;
676676
};
677677

@@ -1336,7 +1336,7 @@ export type ModelWithProperties = {
13361336
number?: number;
13371337
boolean?: boolean;
13381338
reference?: ModelWithString;
1339-
'property with space'?: string;
1339+
propertyWithSpace?: string;
13401340
default?: string;
13411341
try?: string;
13421342
readonly '@namespace.string'?: string;
@@ -1455,11 +1455,11 @@ export type SimpleStringWithPattern = string;
14551455
"
14561456
`;
14571457

1458-
exports[`v2 should generate: ./test/generated/v2/schemas/$_default.ts 1`] = `
1458+
exports[`v2 should generate: ./test/generated/v2/schemas/$_Default.ts 1`] = `
14591459
"/* istanbul ignore file */
14601460
/* tslint:disable */
14611461
/* eslint-disable */
1462-
export const $_default = {
1462+
export const $_Default = {
14631463
properties: {
14641464
name: {
14651465
type: 'string',
@@ -2156,7 +2156,7 @@ export const $ModelWithProperties = {
21562156
reference: {
21572157
type: 'ModelWithString',
21582158
},
2159-
'property with space': {
2159+
propertyWithSpace: {
21602160
type: 'string',
21612161
},
21622162
default: {
@@ -3642,7 +3642,7 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
36423642
export { OpenAPI } from './core/OpenAPI';
36433643
export type { OpenAPIConfig } from './core/OpenAPI';
36443644

3645-
export type { _default as _defaultDto } from './models/_default';
3645+
export type { _Default as _DefaultDto } from './models/_Default';
36463646
export type { ArrayWithArray as ArrayWithArrayDto } from './models/ArrayWithArray';
36473647
export type { ArrayWithBooleans as ArrayWithBooleansDto } from './models/ArrayWithBooleans';
36483648
export type { ArrayWithNumbers as ArrayWithNumbersDto } from './models/ArrayWithNumbers';
@@ -3711,7 +3711,7 @@ export type { SimpleReference as SimpleReferenceDto } from './models/SimpleRefer
37113711
export type { SimpleString as SimpleStringDto } from './models/SimpleString';
37123712
export type { SimpleStringWithPattern as SimpleStringWithPatternDto } from './models/SimpleStringWithPattern';
37133713

3714-
export { $_default } from './schemas/$_default';
3714+
export { $_Default } from './schemas/$_Default';
37153715
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
37163716
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
37173717
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';
@@ -3804,12 +3804,12 @@ export { UploadService } from './services/UploadService';
38043804
"
38053805
`;
38063806

3807-
exports[`v3 should generate: ./test/generated/v3/models/_default.ts 1`] = `
3807+
exports[`v3 should generate: ./test/generated/v3/models/_Default.ts 1`] = `
38083808
"/* istanbul ignore file */
38093809
/* tslint:disable */
38103810
/* eslint-disable */
38113811

3812-
export type _default = {
3812+
export type _Default = {
38133813
name?: string;
38143814
};
38153815

@@ -4380,8 +4380,8 @@ exports[`v3 should generate: ./test/generated/v3/models/File.ts 1`] = `
43804380

43814381
export type File = {
43824382
readonly id?: string;
4383-
readonly updated_at?: string;
4384-
readonly created_at?: string;
4383+
readonly updatedAt?: string;
4384+
readonly createdAt?: string;
43854385
mime: string;
43864386
readonly file?: string;
43874387
};
@@ -4808,7 +4808,7 @@ export type ModelWithProperties = {
48084808
number?: number;
48094809
boolean?: boolean;
48104810
reference?: ModelWithString;
4811-
'property with space'?: string;
4811+
propertyWithSpace?: string;
48124812
default?: string;
48134813
try?: string;
48144814
readonly '@namespace.string'?: string;
@@ -4941,11 +4941,11 @@ export type SimpleStringWithPattern = string | null;
49414941
"
49424942
`;
49434943

4944-
exports[`v3 should generate: ./test/generated/v3/schemas/$_default.ts 1`] = `
4944+
exports[`v3 should generate: ./test/generated/v3/schemas/$_Default.ts 1`] = `
49454945
"/* istanbul ignore file */
49464946
/* tslint:disable */
49474947
/* eslint-disable */
4948-
export const $_default = {
4948+
export const $_Default = {
49494949
properties: {
49504950
name: {
49514951
type: 'string',
@@ -5594,12 +5594,12 @@ export const $File = {
55945594
isReadOnly: true,
55955595
minLength: 1,
55965596
},
5597-
updated_at: {
5597+
updatedAt: {
55985598
type: 'string',
55995599
isReadOnly: true,
56005600
format: 'date-time',
56015601
},
5602-
created_at: {
5602+
createdAt: {
56035603
type: 'string',
56045604
isReadOnly: true,
56055605
format: 'date-time',
@@ -6119,7 +6119,7 @@ export const $ModelWithProperties = {
61196119
reference: {
61206120
type: 'ModelWithString',
61216121
},
6122-
'property with space': {
6122+
propertyWithSpace: {
61236123
type: 'string',
61246124
},
61256125
default: {

0 commit comments

Comments
 (0)