Skip to content

Commit dcb2dd2

Browse files
authored
Merge pull request #53 from acacode/next
Release 1.8.3
2 parents acc770b + 19664f8 commit dcb2dd2

File tree

10 files changed

+670
-121
lines changed

10 files changed

+670
-121
lines changed

.vscode/launch.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@
1414
"port": 9229
1515
},
1616
{
17-
"name": "Debug CLI",
17+
"name": "Debug JSON CLI",
1818
"type": "node",
1919
"request": "launch",
2020
"cwd": "${workspaceFolder}",
2121
"runtimeExecutable": "npm",
22-
"runtimeArgs": ["run-script", "cli:debug"],
22+
"runtimeArgs": ["run-script", "cli:debug:json"],
23+
"port": 9229
24+
},
25+
{
26+
"name": "Debug YAML CLI",
27+
"type": "node",
28+
"request": "launch",
29+
"cwd": "${workspaceFolder}",
30+
"runtimeExecutable": "npm",
31+
"runtimeArgs": ["run-script", "cli:debug:yaml"],
2332
"port": 9229
2433
}
2534
]
26-
}
35+
}

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
# next release
22

3+
# 1.8.3
4+
5+
Fixes:
6+
- Generating invalid code in composed schema contexts ([#51 issue](https://github.com/acacode/swagger-typescript-api/issues/51))
7+
```yaml
8+
components:
9+
schemas:
10+
Test:
11+
type: object
12+
allOf:
13+
- type: object
14+
properties:
15+
x:
16+
type: array
17+
items:
18+
type: string
19+
enum:
20+
- A-B
21+
- type: object
22+
properties:
23+
y:
24+
type: string
25+
```
26+
```ts
27+
export type Test = XAB & { y?: string };
28+
```
29+
330
# 1.8.2
431
532
Fixes:

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "1.8.2",
3+
"version": "1.8.3",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
6-
"cli": "node index.js -r -d -p http://localhost:8080/api/v1/swagger.json -n swagger-test-cli.ts",
7-
"cli:debug": "node --nolazy --inspect-brk=9229 index.js -p ./swagger-test-cli.json -n swagger-test-cli.ts",
6+
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
7+
"cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
8+
"cli:debug:json": "node --nolazy --inspect-brk=9229 index.js -p ./swagger-test-cli.json -n swagger-test-cli.ts",
9+
"cli:debug:yaml": "node --nolazy --inspect-brk=9229 index.js -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
810
"cli:help": "node index.js -h",
911
"test:all": "npm-run-all generate validate test:routeTypes test:noClient test:defaultAsSuccess test:responses --continue-on-error",
1012
"generate": "node tests/generate.js",

src/schema.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,20 @@ const getComplexType = (schema) => {
132132
throw new Error("Uknown complex type");
133133
};
134134

135+
const attachParsedRef = (originalSchema, parsedSchema) => {
136+
if (originalSchema) {
137+
originalSchema.$parsed = parsedSchema;
138+
}
139+
140+
return parsedSchema;
141+
};
142+
135143
const schemaParsers = {
136144
enum: (schema, typeName) => {
137145
const type = getType(schema);
138146
const isIntegerEnum = type === types.number;
139-
return {
147+
148+
return attachParsedRef(schema, {
140149
$parsedSchema: true,
141150
schemaType: "enum",
142151
type: isIntegerEnum ? "intEnum" : "enum",
@@ -148,7 +157,7 @@ const schemaParsers = {
148157
type,
149158
value: isIntegerEnum ? `${key}` : `"${key}"`,
150159
})),
151-
};
160+
});
152161
},
153162
object: (schema, typeName) => {
154163
if (_.isArray(schema.required) && schema.properties) {
@@ -162,7 +171,7 @@ const schemaParsers = {
162171
const properties = _.get(schema, "properties");
163172
const typeContent = getObjectTypeContent(properties);
164173

165-
return {
174+
return attachParsedRef(schema, {
166175
$parsedSchema: true,
167176
schemaType: "object",
168177
type: "object",
@@ -171,20 +180,20 @@ const schemaParsers = {
171180
description: formatDescription(schema.description),
172181
allFieldsAreOptional: !_.some(_.values(typeContent), (part) => part.isRequired),
173182
content: typeContent,
174-
};
183+
});
175184
},
176185
complex: (schema, typeName) => {
177186
const complexType = getComplexType(schema);
178187

179-
return {
188+
return attachParsedRef(schema, {
180189
$parsedSchema: true,
181190
schemaType: "complex",
182191
type: "type",
183192
typeIdentifier: "type",
184193
name: typeName,
185194
description: formatDescription(schema.description),
186195
content: complexSchemaParsers[complexType](schema),
187-
};
196+
});
188197
},
189198
primitive: (schema, typeName) => {
190199
let contentType = null;
@@ -195,7 +204,7 @@ const schemaParsers = {
195204
contentType = `Record<string, ${fieldType}>`;
196205
}
197206

198-
return {
207+
return attachParsedRef(schema, {
199208
$parsedSchema: true,
200209
schemaType: "primitive",
201210
type: "primitive",
@@ -204,7 +213,7 @@ const schemaParsers = {
204213
description: formatDescription(description),
205214
// TODO: probably it should be refactored. `type === 'null'` is not flexible
206215
content: type === "null" ? type : contentType || getType(schema),
207-
};
216+
});
208217
},
209218
};
210219

@@ -227,9 +236,9 @@ const parseSchema = (rawSchema, typeName, formattersMap) => {
227236
return rawSchema;
228237
}
229238

230-
if (rawSchema.$parsedSchema) {
231-
schemaType = rawSchema.schemaType;
232-
parsedSchema = rawSchema;
239+
if (rawSchema.$parsed) {
240+
schemaType = rawSchema.$parsed.schemaType;
241+
parsedSchema = rawSchema.$parsed;
233242
} else {
234243
const fixedRawSchema = checkAndFixSchema(rawSchema);
235244
schemaType = getInternalSchemaType(fixedRawSchema);

src/typeFormatters.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ const formatters = {
2828
return `${commonText}${result}`;
2929
}).join(""),
3030
type: (content) => {
31-
if (content.includes(" & ")) {
32-
return content.split(" & ").map(checkAndRenameModelName).join(" & ");
33-
}
31+
// const separators = [" & ", " | "];
3432

35-
if (content.includes(" | ")) {
36-
return content.split(" | ").map(checkAndRenameModelName).join(" | ");
37-
}
33+
// for (const separator of separators) {
34+
// if (content.includes(separator)) {
35+
// return content.split(separator).map(checkAndRenameModelName).join(separator);
36+
// }
37+
// }
3838

3939
return content;
4040
},

0 commit comments

Comments
 (0)