Skip to content

Commit e7aee32

Browse files
committed
resolve conflicts
2 parents daaea3a + 1830f73 commit e7aee32

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+712
-637
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"@typescript-eslint/explicit-module-boundary-types": 0,
2525
"sort-imports": "off",
2626
"import/order": "off",
27-
"simple-import-sort/sort": "error",
27+
"simple-import-sort/imports": "error",
28+
"simple-import-sort/exports": "error",
2829
"prettier/prettier": ["error"]
2930
}
3031
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.6.0",
3+
"version": "0.7.0-beta",
44
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

src/client/interfaces/Model.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ import type { Schema } from './Schema';
33

44
export interface Model extends Schema {
55
name: string;
6-
export: 'reference' | 'generic' | 'enum' | 'array' | 'dictionary' | 'interface';
6+
export: 'reference' | 'generic' | 'enum' | 'array' | 'dictionary' | 'interface' | 'one-of' | 'any-of' | 'all-of';
77
type: string;
88
base: string;
99
template: string | null;
1010
link: Model | null;
1111
description: string | null;
1212
default?: string;
1313
imports: string[];
14-
extends: string[];
1514
enum: Enum[];
1615
enums: Model[];
1716
properties: Model[];
18-
extendedFrom?: string[];
19-
extendedBy?: string[];
2017
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Model } from './Model';
2+
3+
export interface ModelComposition {
4+
type: 'one-of' | 'any-of' | 'all-of';
5+
imports: string[];
6+
enums: Model[];
7+
properties: Model[];
8+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export enum HttpClient {
1313
NODE = 'node',
1414
}
1515

16-
export interface Options {
16+
export type Options = {
1717
input: string | Record<string, any>;
1818
output: string;
1919
httpClient?: HttpClient;
@@ -24,7 +24,7 @@ export interface Options {
2424
exportModels?: boolean;
2525
exportSchemas?: boolean;
2626
write?: boolean;
27-
}
27+
};
2828

2929
/**
3030
* Generate the OpenAPI client. This method will read the OpenAPI specification and based on the

src/openApi/v2/interfaces/OpenApiParameter.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { WithEnumExtension } from './Extensions/WithEnumExtension';
2-
import { WithNullableExtension } from './Extensions/WithNullableExtension';
2+
import type { WithNullableExtension } from './Extensions/WithNullableExtension';
33
import type { OpenApiItems } from './OpenApiItems';
44
import type { OpenApiReference } from './OpenApiReference';
55
import type { OpenApiSchema } from './OpenApiSchema';

src/openApi/v2/parser/constants.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/openApi/v2/parser/extendEnum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export function extendEnum(enumerators: Enum[], definition: WithEnumExtension):
1212
const descriptions = definition['x-enum-descriptions'];
1313

1414
return enumerators.map((enumerator, index) => ({
15-
name: (names && names[index]) || enumerator.name,
16-
description: (descriptions && descriptions[index]) || enumerator.description,
15+
name: names?.[index] || enumerator.name,
16+
description: descriptions?.[index] || enumerator.description,
1717
value: enumerator.value,
1818
type: enumerator.type,
1919
}));

src/openApi/v2/parser/getEnum.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Enum } from '../../../client/interfaces/Enum';
2-
import { PrimaryType } from './constants';
32
import { isDefined } from './isDefined';
43

54
export function getEnum(values?: (string | number)[]): Enum[] {
@@ -14,7 +13,7 @@ export function getEnum(values?: (string | number)[]): Enum[] {
1413
return {
1514
name: `_${value}`,
1615
value: String(value),
17-
type: PrimaryType.NUMBER,
16+
type: 'number',
1817
description: null,
1918
};
2019
}
@@ -25,7 +24,7 @@ export function getEnum(values?: (string | number)[]): Enum[] {
2524
.replace(/([a-z])([A-Z]+)/g, '$1_$2')
2625
.toUpperCase(),
2726
value: `'${value}'`,
28-
type: PrimaryType.STRING,
27+
type: 'string',
2928
description: null,
3029
};
3130
});

src/openApi/v2/parser/getEnumFromDescription.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Enum } from '../../../client/interfaces/Enum';
2-
import { PrimaryType } from './constants';
32

43
export function getEnumFromDescription(description: string): Enum[] {
54
// Check if we can find this special format string:
@@ -20,7 +19,7 @@ export function getEnumFromDescription(description: string): Enum[] {
2019
.replace(/([a-z])([A-Z]+)/g, '$1_$2')
2120
.toUpperCase(),
2221
value: String(value),
23-
type: PrimaryType.NUMBER,
22+
type: 'number',
2423
description: null,
2524
});
2625
}

src/openApi/v2/parser/getMappedType.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1-
import { PrimaryType, TYPE_MAPPINGS } from './constants';
1+
export const TYPE_MAPPINGS = new Map<string, string>([
2+
['File', 'File'],
3+
['file', 'File'],
4+
['any', 'any'],
5+
['object', 'any'],
6+
['array', 'any[]'],
7+
['boolean', 'boolean'],
8+
['byte', 'number'],
9+
['int', 'number'],
10+
['integer', 'number'],
11+
['float', 'number'],
12+
['double', 'number'],
13+
['short', 'number'],
14+
['long', 'number'],
15+
['number', 'number'],
16+
['char', 'string'],
17+
['date', 'string'],
18+
['date-time', 'string'],
19+
['password', 'string'],
20+
['string', 'string'],
21+
['void', 'void'],
22+
['null', 'null'],
23+
]);
224

325
/**
426
* Get mapped type for given type to any basic Typescript/Javascript type.
527
*/
6-
export function getMappedType(type: string): PrimaryType | undefined {
28+
export function getMappedType(type: string): string | undefined {
729
return TYPE_MAPPINGS.get(type);
830
}
931

src/openApi/v2/parser/getModel.ts

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import type { Model } from '../../../client/interfaces/Model';
22
import type { OpenApi } from '../interfaces/OpenApi';
33
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
4-
import { PrimaryType } from './constants';
54
import { extendEnum } from './extendEnum';
65
import { getComment } from './getComment';
76
import { getEnum } from './getEnum';
87
import { getEnumFromDescription } from './getEnumFromDescription';
8+
import { getModelComposition } from './getModelComposition';
99
import { getModelProperties } from './getModelProperties';
1010
import { getPattern } from './getPattern';
1111
import { getType } from './getType';
1212

1313
export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefinition: boolean = false, name: string = ''): Model {
1414
const model: Model = {
15-
name: name,
15+
name,
1616
export: 'interface',
17-
type: PrimaryType.OBJECT,
18-
base: PrimaryType.OBJECT,
17+
type: 'any',
18+
base: 'any',
1919
template: null,
2020
link: null,
2121
description: getComment(definition.description),
22-
isDefinition: isDefinition,
22+
isDefinition,
2323
isReadOnly: definition.readOnly === true,
2424
isNullable: definition['x-nullable'] === true,
2525
isRequired: false,
@@ -38,7 +38,6 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti
3838
minProperties: definition.minProperties,
3939
pattern: getPattern(definition.pattern),
4040
imports: [],
41-
extends: [],
4241
enum: [],
4342
enums: [],
4443
properties: [],
@@ -59,8 +58,8 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti
5958
const extendedEnumerators = extendEnum(enumerators, definition);
6059
if (extendedEnumerators.length) {
6160
model.export = 'enum';
62-
model.type = PrimaryType.STRING;
63-
model.base = PrimaryType.STRING;
61+
model.type = 'string';
62+
model.base = 'string';
6463
model.enum.push(...extendedEnumerators);
6564
return model;
6665
}
@@ -70,8 +69,8 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti
7069
const enumerators = getEnumFromDescription(definition.description);
7170
if (enumerators.length) {
7271
model.export = 'enum';
73-
model.type = PrimaryType.NUMBER;
74-
model.base = PrimaryType.NUMBER;
72+
model.type = 'number';
73+
model.base = 'number';
7574
model.enum.push(...enumerators);
7675
return model;
7776
}
@@ -98,7 +97,7 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti
9897
}
9998
}
10099

101-
if (definition.type === 'object' && definition.additionalProperties && typeof definition.additionalProperties === 'object') {
100+
if (definition.type === 'object' && typeof definition.additionalProperties === 'object') {
102101
if (definition.additionalProperties.$ref) {
103102
const additionalProperties = getType(definition.additionalProperties.$ref);
104103
model.export = 'dictionary';
@@ -119,42 +118,30 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti
119118
}
120119
}
121120

122-
if (definition.type === 'object' || definition.allOf) {
123-
model.export = 'interface';
124-
model.type = PrimaryType.OBJECT;
125-
model.base = PrimaryType.OBJECT;
121+
if (definition.allOf?.length) {
122+
const composition = getModelComposition(openApi, definition.allOf, 'all-of', getModel);
123+
model.export = composition.type;
124+
model.imports.push(...composition.imports);
125+
model.enums.push(...composition.enums);
126+
model.properties.push(...composition.properties);
127+
return model;
128+
}
126129

127-
if (definition.allOf && definition.allOf.length) {
128-
definition.allOf.forEach(parent => {
129-
if (parent.$ref) {
130-
const parentRef = getType(parent.$ref);
131-
model.extends.push(parentRef.base);
132-
model.imports.push(parentRef.base);
133-
}
134-
if (parent.type === 'object' && parent.properties) {
135-
const properties = getModelProperties(openApi, parent, getModel);
136-
properties.forEach(property => {
137-
model.properties.push(property);
138-
model.imports.push(...property.imports);
139-
if (property.export === 'enum') {
140-
model.enums.push(property);
141-
}
142-
});
143-
}
144-
});
145-
}
130+
if (definition.type === 'object') {
131+
model.export = 'interface';
132+
model.type = 'any';
133+
model.base = 'any';
146134

147135
if (definition.properties) {
148136
const properties = getModelProperties(openApi, definition, getModel);
149137
properties.forEach(property => {
150-
model.properties.push(property);
151138
model.imports.push(...property.imports);
139+
model.properties.push(property);
152140
if (property.export === 'enum') {
153141
model.enums.push(property);
154142
}
155143
});
156144
}
157-
158145
return model;
159146
}
160147

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { ModelComposition } from '../../../client/interfaces/ModelComposition';
2+
import type { OpenApi } from '../interfaces/OpenApi';
3+
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
4+
import type { getModel } from './getModel';
5+
6+
// Fix for circular dependency
7+
export type GetModelFn = typeof getModel;
8+
9+
export function getModelComposition(openApi: OpenApi, definitions: OpenApiSchema[], type: 'one-of' | 'any-of' | 'all-of', getModel: GetModelFn): ModelComposition {
10+
const composition: ModelComposition = {
11+
type,
12+
imports: [],
13+
enums: [],
14+
properties: [],
15+
};
16+
17+
const modes = definitions.map(definition => getModel(openApi, definition));
18+
modes.forEach(model => {
19+
composition.imports.push(...model.imports);
20+
composition.enums.push(...model.enums);
21+
composition.properties.push(model);
22+
});
23+
24+
return composition;
25+
}

src/openApi/v2/parser/getModelProperties.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import type { OpenApi } from '../interfaces/OpenApi';
33
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
44
import { escapeName } from './escapeName';
55
import { getComment } from './getComment';
6+
import type { getModel } from './getModel';
67
import { getPattern } from './getPattern';
78
import { getType } from './getType';
89

9-
// Fix for circular dependency between getModel and getModelProperties
10-
export type GetModel = (openApi: OpenApi, definition: OpenApiSchema, isDefinition?: boolean, name?: string) => Model;
10+
// Fix for circular dependency
11+
export type GetModelFn = typeof getModel;
1112

12-
export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema, getModel: GetModel): Model[] {
13+
export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema, getModel: GetModelFn): Model[] {
1314
const models: Model[] = [];
1415
for (const propertyName in definition.properties) {
1516
if (definition.properties.hasOwnProperty(propertyName)) {
1617
const property = definition.properties[propertyName];
17-
const propertyRequired = definition.required && definition.required.includes(propertyName);
18+
const propertyRequired = definition.required?.includes(propertyName);
1819
if (property.$ref) {
1920
const model = getType(property.$ref);
2021
models.push({
@@ -44,7 +45,6 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
4445
minProperties: property.minProperties,
4546
pattern: getPattern(property.pattern),
4647
imports: model.imports,
47-
extends: [],
4848
enum: [],
4949
enums: [],
5050
properties: [],
@@ -78,7 +78,6 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
7878
minProperties: property.minProperties,
7979
pattern: getPattern(property.pattern),
8080
imports: model.imports,
81-
extends: model.extends,
8281
enum: model.enum,
8382
enums: model.enums,
8483
properties: model.properties,

0 commit comments

Comments
 (0)