Skip to content

Commit fe1a325

Browse files
author
Andy Hite
committed
fix: fix casing of imports
1 parent 9675fbb commit fe1a325

13 files changed

+83
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shipengine/openapi-typescript-codegen",
3-
"version": "0.23.0",
3+
"version": "0.23.1",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import camelCase, { Options as CamelCaseOptions } from 'camelcase';
2+
3+
export type { CamelCaseOptions };
4+
5+
export const camelCaseName = (value: string, options?: CamelCaseOptions) => {
6+
return value
7+
.split('.')
8+
.map(part => {
9+
const prefix = part[0] === '_' ? '_' : '';
10+
return `${prefix}${camelCase(part, options)}`;
11+
})
12+
.join('.');
13+
};

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(`fooBar`);
8-
expect(escapeName('foo bar')).toEqual(`fooBar`);
9-
expect(escapeName('foo-bar')).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'`);
1010
expect(escapeName('foo.bar')).toEqual(`'foo.bar'`);
11-
expect(escapeName('foo_bar')).toEqual('fooBar');
12-
expect(escapeName('123foo.bar')).toEqual(`'123Foo.bar'`);
11+
expect(escapeName('foo_bar')).toEqual('foo_bar');
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: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import camelCase from 'camelcase';
1+
import { camelCaseName, CamelCaseOptions } from './camelCaseName';
22

33
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-
124
if (value || value === '') {
135
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
146
if (!validName) {
@@ -18,3 +10,8 @@ export const escapeName = (value: string): string => {
1810

1911
return value;
2012
};
13+
14+
export const escapeAndCamelizeName = (value: string, options?: CamelCaseOptions): string => {
15+
value = camelCaseName(value, options);
16+
return escapeName(value);
17+
};

src/openApi/v2/parser/getModel.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import camelCase from 'camelcase';
2-
31
import type { Model } from '../../../client/interfaces/Model';
42
import { getPattern } from '../../../utils/getPattern';
53
import type { OpenApi } from '../interfaces/OpenApi';
64
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
5+
import { camelCaseName } from './camelCaseName';
76
import { extendEnum } from './extendEnum';
87
import { getEnum } from './getEnum';
98
import { getModelComposition } from './getModelComposition';
@@ -16,10 +15,8 @@ export const getModel = (
1615
isDefinition: boolean = false,
1716
name: string = ''
1817
): Model => {
19-
const prefix = name[0] === '_' ? '_' : '';
20-
2118
const model: Model = {
22-
name: `${prefix}${camelCase(name, { pascalCase: true })}`,
19+
name: camelCaseName(name, { pascalCase: true }),
2320
export: 'interface',
2421
type: 'any',
2522
base: 'any',
@@ -56,7 +53,7 @@ export const getModel = (
5653
model.type = definitionRef.type;
5754
model.base = definitionRef.base;
5855
model.template = definitionRef.template;
59-
model.imports.push(...definitionRef.imports);
56+
model.imports.push(...definitionRef.imports.map(x => camelCaseName(x, { pascalCase: true })));
6057
return model;
6158
}
6259

@@ -79,7 +76,7 @@ export const getModel = (
7976
model.type = arrayItems.type;
8077
model.base = arrayItems.base;
8178
model.template = arrayItems.template;
82-
model.imports.push(...arrayItems.imports);
79+
model.imports.push(...arrayItems.imports.map(x => camelCaseName(x, { pascalCase: true })));
8380
return model;
8481
} else {
8582
const arrayItems = getModel(openApi, definition.items);
@@ -88,7 +85,7 @@ export const getModel = (
8885
model.base = arrayItems.base;
8986
model.template = arrayItems.template;
9087
model.link = arrayItems;
91-
model.imports.push(...arrayItems.imports);
88+
model.imports.push(...arrayItems.imports.map(x => camelCaseName(x, { pascalCase: true })));
9289
return model;
9390
}
9491
}
@@ -100,7 +97,7 @@ export const getModel = (
10097
model.type = additionalProperties.type;
10198
model.base = additionalProperties.base;
10299
model.template = additionalProperties.template;
103-
model.imports.push(...additionalProperties.imports);
100+
model.imports.push(...additionalProperties.imports.map(x => camelCaseName(x, { pascalCase: true })));
104101
return model;
105102
} else {
106103
const additionalProperties = getModel(openApi, definition.additionalProperties);
@@ -109,15 +106,15 @@ export const getModel = (
109106
model.base = additionalProperties.base;
110107
model.template = additionalProperties.template;
111108
model.link = additionalProperties;
112-
model.imports.push(...additionalProperties.imports);
109+
model.imports.push(...additionalProperties.imports.map(x => camelCaseName(x, { pascalCase: true })));
113110
return model;
114111
}
115112
}
116113

117114
if (definition.allOf?.length) {
118115
const composition = getModelComposition(openApi, definition, definition.allOf, 'all-of', getModel);
119116
model.export = composition.type;
120-
model.imports.push(...composition.imports);
117+
model.imports.push(...composition.imports.map(x => camelCaseName(x, { pascalCase: true })));
121118
model.properties.push(...composition.properties);
122119
model.enums.push(...composition.enums);
123120
return model;
@@ -131,7 +128,7 @@ export const getModel = (
131128
if (definition.properties) {
132129
const modelProperties = getModelProperties(openApi, definition, getModel);
133130
modelProperties.forEach(modelProperty => {
134-
model.imports.push(...modelProperty.imports);
131+
model.imports.push(...modelProperty.imports.map(x => camelCaseName(x, { pascalCase: true })));
135132
model.enums.push(...modelProperty.enums);
136133
model.properties.push(modelProperty);
137134
if (modelProperty.export === 'enum') {
@@ -149,7 +146,7 @@ export const getModel = (
149146
model.type = definitionType.type;
150147
model.base = definitionType.base;
151148
model.template = definitionType.template;
152-
model.imports.push(...definitionType.imports);
149+
model.imports.push(...definitionType.imports.map(x => camelCaseName(x, { pascalCase: true })));
153150
return model;
154151
}
155152

src/openApi/v2/parser/getModelComposition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +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';
5+
import { escapeAndCamelizeName } from './escapeName';
66
import type { getModel } from './getModel';
77
import { getModelProperties } from './getModelProperties';
88
import { getRequiredPropertiesFromComposition } from './getRequiredPropertiesFromComposition';
@@ -44,7 +44,7 @@ export const getModelComposition = (
4444
if (definition.required) {
4545
const requiredProperties = getRequiredPropertiesFromComposition(
4646
openApi,
47-
definition.required.map(required => escapeName(required)),
47+
definition.required.map(required => escapeAndCamelizeName(required)),
4848
definitions,
4949
getModel
5050
);

src/openApi/v2/parser/getModelProperties.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Model } from '../../../client/interfaces/Model';
22
import { getPattern } from '../../../utils/getPattern';
33
import type { OpenApi } from '../interfaces/OpenApi';
44
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
5-
import { escapeName } from './escapeName';
5+
import { escapeAndCamelizeName } from './escapeName';
66
import type { getModel } from './getModel';
77
import { getType } from './getType';
88

@@ -18,7 +18,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema,
1818
if (property.$ref) {
1919
const model = getType(property.$ref);
2020
models.push({
21-
name: escapeName(propertyName),
21+
name: escapeAndCamelizeName(propertyName),
2222
export: 'reference',
2323
type: model.type,
2424
base: model.base,
@@ -51,7 +51,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema,
5151
} else {
5252
const model = getModel(openApi, property);
5353
models.push({
54-
name: escapeName(propertyName),
54+
name: escapeAndCamelizeName(propertyName),
5555
export: model.export,
5656
type: model.type,
5757
base: model.base,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import camelCase, { Options as CamelCaseOptions } from 'camelcase';
2+
3+
export type { CamelCaseOptions };
4+
5+
export const camelCaseName = (value: string, options?: CamelCaseOptions) => {
6+
return value
7+
.split('.')
8+
.map(part => {
9+
const prefix = part[0] === '_' ? '_' : '';
10+
return `${prefix}${camelCase(part, options)}`;
11+
})
12+
.join('.');
13+
};

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(`fooBar`);
8-
expect(escapeName('foo bar')).toEqual(`fooBar`);
9-
expect(escapeName('foo-bar')).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'`);
1010
expect(escapeName('foo.bar')).toEqual(`'foo.bar'`);
11-
expect(escapeName('foo_bar')).toEqual('fooBar');
12-
expect(escapeName('123foo.bar')).toEqual(`'123Foo.bar'`);
11+
expect(escapeName('foo_bar')).toEqual('foo_bar');
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: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import camelCase from 'camelcase';
1+
import { camelCaseName, CamelCaseOptions } from './camelCaseName';
22

33
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-
124
if (value || value === '') {
135
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
146
if (!validName) {
@@ -18,3 +10,8 @@ export const escapeName = (value: string): string => {
1810

1911
return value;
2012
};
13+
14+
export const escapeAndCamelizeName = (value: string, options?: CamelCaseOptions): string => {
15+
value = camelCaseName(value, options);
16+
return escapeName(value);
17+
};

0 commit comments

Comments
 (0)