Skip to content

Commit 7d81ef2

Browse files
committed
fix: pascal case
1 parent 0b2a380 commit 7d81ef2

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
FROM node:alpine
22
WORKDIR /usr/src/openapi
3+
4+
COPY ["package.json", "package-lock.json", "./"]
5+
RUN ["npm", "install"]
6+
37
COPY . /usr/src/openapi
4-
RUN npm install
58
RUN npm run release
69
ENTRYPOINT [ "node", "/usr/src/openapi/bin/index.js" ]
710
CMD "--help"

src/openApi/v2/parser/getModel.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Model } from '../../../client/interfaces/Model';
22
import { getPattern } from '../../../utils/getPattern';
3+
import { toPascalCase } from '../../../utils/toPascalCase';
34
import type { OpenApi } from '../interfaces/OpenApi';
45
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
56
import { extendEnum } from './extendEnum';
@@ -49,10 +50,10 @@ export const getModel = (
4950
if (definition.$ref) {
5051
const definitionRef = getType(definition.$ref);
5152
model.export = 'reference';
52-
model.type = definitionRef.type;
53-
model.base = definitionRef.base;
53+
model.type = toPascalCase(definitionRef.type);
54+
model.base = toPascalCase(definitionRef.base);
5455
model.template = definitionRef.template;
55-
model.imports.push(...definitionRef.imports);
56+
model.imports.push(...definitionRef.imports.map(toPascalCase));
5657
return model;
5758
}
5859

@@ -72,10 +73,10 @@ export const getModel = (
7273
if (definition.items.$ref) {
7374
const arrayItems = getType(definition.items.$ref);
7475
model.export = 'array';
75-
model.type = arrayItems.type;
76-
model.base = arrayItems.base;
76+
model.type = toPascalCase(arrayItems.type);
77+
model.base = toPascalCase(arrayItems.base);
7778
model.template = arrayItems.template;
78-
model.imports.push(...arrayItems.imports);
79+
model.imports.push(...arrayItems.imports.map(toPascalCase));
7980
return model;
8081
} else {
8182
const arrayItems = getModel(openApi, definition.items);
@@ -93,10 +94,10 @@ export const getModel = (
9394
if (definition.additionalProperties.$ref) {
9495
const additionalProperties = getType(definition.additionalProperties.$ref);
9596
model.export = 'dictionary';
96-
model.type = additionalProperties.type;
97-
model.base = additionalProperties.base;
97+
model.type = toPascalCase(additionalProperties.type);
98+
model.base = toPascalCase(additionalProperties.base);
9899
model.template = additionalProperties.template;
99-
model.imports.push(...additionalProperties.imports);
100+
model.imports.push(...additionalProperties.imports.map(toPascalCase));
100101
return model;
101102
} else {
102103
const additionalProperties = getModel(openApi, definition.additionalProperties);

src/openApi/v2/parser/getModelProperties.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Model } from '../../../client/interfaces/Model';
22
import { getPattern } from '../../../utils/getPattern';
3+
import { toPascalCase } from '../../../utils/toPascalCase';
34
import type { OpenApi } from '../interfaces/OpenApi';
45
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
56
import { escapeName } from './escapeName';
@@ -17,11 +18,12 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema,
1718
const propertyRequired = !!definition.required?.includes(propertyName);
1819
if (property.$ref) {
1920
const model = getType(property.$ref);
21+
console.log(model);
2022
models.push({
2123
name: escapeName(propertyName),
2224
export: 'reference',
23-
type: model.type,
24-
base: model.base,
25+
type: toPascalCase(model.type),
26+
base: toPascalCase(model.base),
2527
template: model.template,
2628
link: null,
2729
description: property.description || null,
@@ -43,7 +45,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema,
4345
maxProperties: property.maxProperties,
4446
minProperties: property.minProperties,
4547
pattern: getPattern(property.pattern),
46-
imports: model.imports,
48+
imports: model.imports.map(toPascalCase),
4749
enum: [],
4850
enums: [],
4951
properties: [],

src/openApi/v2/parser/getModels.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Model } from '../../../client/interfaces/Model';
22
import { reservedWords } from '../../../utils/reservedWords';
3+
import { toPascalCase } from '../../../utils/toPascalCase';
34
import type { OpenApi } from '../interfaces/OpenApi';
45
import { getModel } from './getModel';
56
import { getType } from './getType';
@@ -10,7 +11,12 @@ export const getModels = (openApi: OpenApi): Model[] => {
1011
if (openApi.definitions.hasOwnProperty(definitionName)) {
1112
const definition = openApi.definitions[definitionName];
1213
const definitionType = getType(definitionName);
13-
const model = getModel(openApi, definition, true, definitionType.base.replace(reservedWords, '_$1'));
14+
const model = getModel(
15+
openApi,
16+
definition,
17+
true,
18+
toPascalCase(definitionType.base.replace(reservedWords, '_$1'))
19+
);
1420
models.push(model);
1521
}
1622
}

src/utils/toPascalCase.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const _capitalize = (string: string): string => {
2+
return string.slice(0, 1).toUpperCase() + string.slice(1, string.length);
3+
};
4+
5+
export const toPascalCase = (str: string): string => {
6+
return str
7+
.split('_')
8+
.map(str => _capitalize(str.split('/').map(_capitalize).join('/')))
9+
.join('')
10+
.replace(/(?:^\w|[A-Z]|\b\w)/g, word => word.toUpperCase())
11+
.replace(/\s+/g, '');
12+
};

0 commit comments

Comments
 (0)