Skip to content

Commit 2fdcf73

Browse files
authored
Release 1.6.2 (#38)
* feat: handle nullable option for primitive types (#37) feat: handle nullable option for primitive types (#36) Co-authored-by: svolkov <[email protected]> * bump: up version to 1.6.2; chore: update swagger2openapi to latest (6.0.0); docs: update CHANGELOG Co-authored-by: svolkov <[email protected]>
1 parent 10d6e9c commit 2fdcf73

File tree

9 files changed

+143
-119
lines changed

9 files changed

+143
-119
lines changed

CHANGELOG.md

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

3+
# 1.6.2
4+
5+
Fixes:
6+
7+
- Nullable not included in type definition ([issue](https://github.com/acacode/swagger-typescript-api/issues/36))
8+
9+
Internal:
10+
11+
- Update `swagger2openapi`(`6.0.0`) dependency
12+
313
# 1.6.1
414

515
Internal:

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "1.6.1",
3+
"version": "1.6.2",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
66
"cli": "node index.js -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
@@ -36,7 +36,7 @@
3636
"lodash": "^4.17.15",
3737
"mustache": "^4.0.0",
3838
"prettier": "^2.0.2",
39-
"swagger2openapi": "^5.4.0"
39+
"swagger2openapi": "^6.0.0"
4040
},
4141
"bin": {
4242
"swagger-typescript-api": "index.js",

src/schema.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ const typeAliases = {
1111
integer: "number",
1212
};
1313

14-
const findSchemaType = schema => {
14+
const findSchemaType = (schema) => {
1515
if (schema.enum) return "enum";
1616
if (schema.properties) return "object";
1717
if (schema.allOf || schema.oneOf || schema.anyOf || schema.not) return "complex";
1818

1919
return "primitive";
2020
};
2121

22-
const getPrimitiveType = property => {
23-
const type = _.get(property, "type");
24-
return typeAliases[type] || type || DEFAULT_PRIMITIVE_TYPE;
22+
const getPrimitiveType = (property) => {
23+
const { type, nullable } = property || {};
24+
const primitiveType = typeAliases[type] || type;
25+
return primitiveType
26+
? (nullable && `${primitiveType} | null`) || primitiveType
27+
: DEFAULT_PRIMITIVE_TYPE;
2528
};
2629

2730
const specificObjectTypes = {
@@ -31,24 +34,24 @@ const specificObjectTypes = {
3134
},
3235
};
3336

34-
const getRefType = property => {
37+
const getRefType = (property) => {
3538
const ref = property && property["$ref"];
3639
return (ref && config.componentsMap[ref]) || null;
3740
};
3841

39-
const getRefTypeName = property => {
42+
const getRefTypeName = (property) => {
4043
const refTypeInfo = getRefType(property);
4144
return refTypeInfo && checkAndRenameModelName(refTypeInfo.typeName);
4245
};
4346

44-
const getType = property => {
47+
const getType = (property) => {
4548
if (!property) return DEFAULT_PRIMITIVE_TYPE;
4649

4750
const anotherTypeGetter = specificObjectTypes[property.type] || getPrimitiveType;
4851
return getRefTypeName(property) || anotherTypeGetter(property);
4952
};
5053

51-
const getObjectTypeContent = properties => {
54+
const getObjectTypeContent = (properties) => {
5255
return _.map(properties, (property, name) => {
5356
// TODO: probably nullable should'n be use as required/no-required conditions
5457
const isRequired =
@@ -66,27 +69,27 @@ const getObjectTypeContent = properties => {
6669
const complexTypeGetter = ({ description, ...schema }) => getInlineParseContent(schema);
6770

6871
const complexSchemaParsers = {
69-
oneOf: schema => {
72+
oneOf: (schema) => {
7073
// T1 | T2
7174
const combined = _.map(schema.oneOf, complexTypeGetter);
7275
return combined.join(" | ");
7376
},
74-
allOf: schema => {
77+
allOf: (schema) => {
7578
// T1 & T2
7679
return _.map(schema.allOf, complexTypeGetter).join(" & ");
7780
},
78-
anyOf: schema => {
81+
anyOf: (schema) => {
7982
// T1 | T2 | (T1 & T2)
8083
const combined = _.map(schema.anyOf, complexTypeGetter);
8184
return `${combined.join(" | ")}` + (combined.length > 1 ? ` | (${combined.join(" & ")})` : "");
8285
},
8386
// TODO
84-
not: schema => {
87+
not: (schema) => {
8588
// TODO
8689
},
8790
};
8891

89-
const getComplexType = schema => {
92+
const getComplexType = (schema) => {
9093
if (schema.oneOf) return "oneOf";
9194
if (schema.allOf) return "allOf";
9295
if (schema.anyOf) return "anyOf";
@@ -108,7 +111,7 @@ const schemaParsers = {
108111
typeIdentifier: isIntegerEnum ? "type" : "enum",
109112
name: typeName,
110113
description: formatDescription(schema.description),
111-
content: _.map(schema.enum, key => ({
114+
content: _.map(schema.enum, (key) => ({
112115
key,
113116
type,
114117
value: isIntegerEnum ? `${key}` : `"${key}"`,
@@ -117,7 +120,7 @@ const schemaParsers = {
117120
},
118121
object: (schema, typeName) => {
119122
if (_.isArray(schema.required) && schema.properties) {
120-
schema.required.forEach(requiredFieldName => {
123+
schema.required.forEach((requiredFieldName) => {
121124
if (schema.properties[requiredFieldName]) {
122125
schema.properties[requiredFieldName].required = true;
123126
}
@@ -134,7 +137,7 @@ const schemaParsers = {
134137
typeIdentifier: "interface",
135138
name: typeName,
136139
description: formatDescription(schema.description),
137-
allFieldsAreOptional: !_.some(_.values(typeContent), part => part.isRequired),
140+
allFieldsAreOptional: !_.some(_.values(typeContent), (part) => part.isRequired),
138141
content: typeContent,
139142
};
140143
},
@@ -172,7 +175,7 @@ const schemaParsers = {
172175
},
173176
};
174177

175-
const checkAndFixSchema = schema => {
178+
const checkAndFixSchema = (schema) => {
176179
if (schema.items && !schema.type) {
177180
schema.type = "array";
178181
}
@@ -208,10 +211,10 @@ const parseSchema = (rawSchema, typeName, formattersMap) => {
208211
);
209212
};
210213

211-
const parseSchemas = components =>
214+
const parseSchemas = (components) =>
212215
_.map(_.get(components, "schemas"), (schema, typeName) => parseSchema(schema, typeName));
213216

214-
const getInlineParseContent = rawTypeData =>
217+
const getInlineParseContent = (rawTypeData) =>
215218
parseSchema(rawTypeData, null, inlineExtraFormatters).content;
216219

217220
module.exports = {

src/typeFormatters.js

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,65 @@
1-
const _ = require('lodash');
1+
const _ = require("lodash");
22
const { checkAndRenameModelName } = require("./modelNames");
33

44
const formatters = {
5-
'enum': content => _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(',\n'),
6-
'intEnum': content => _.map(content, ({ value }) => value).join(' | '),
7-
'object': content => _.map(content, part => {
8-
const extraSpace = ' '
9-
const result = `${extraSpace}${part.field};\n`;
10-
11-
const comments = [
12-
part.title,
13-
part.description
14-
].filter(Boolean)
15-
16-
const commonText = comments.length ? [
17-
'',
18-
'/**',
19-
...comments.reduce((acc, comment) => [...acc, ...comment.split(/\n/g).map(part => ` * ${part}`)], []),
20-
' */'
21-
].map(part => `${extraSpace}${part}\n`).join('') : '';
22-
23-
return `${commonText}${result}`;
24-
}).join(''),
25-
'type': content => {
26-
if (content.includes(' & ')) {
27-
return content.split(' & ').map(checkAndRenameModelName).join(' & ')
5+
enum: (content) => _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(",\n"),
6+
intEnum: (content) => _.map(content, ({ value }) => value).join(" | "),
7+
object: (content) =>
8+
_.map(content, (part) => {
9+
const extraSpace = " ";
10+
const result = `${extraSpace}${part.field};\n`;
11+
12+
const comments = [part.title, part.description].filter(Boolean);
13+
14+
const commonText = comments.length
15+
? [
16+
"",
17+
"/**",
18+
...comments.reduce(
19+
(acc, comment) => [...acc, ...comment.split(/\n/g).map((part) => ` * ${part}`)],
20+
[],
21+
),
22+
" */",
23+
]
24+
.map((part) => `${extraSpace}${part}\n`)
25+
.join("")
26+
: "";
27+
28+
return `${commonText}${result}`;
29+
}).join(""),
30+
type: (content) => {
31+
if (content.includes(" & ")) {
32+
return content.split(" & ").map(checkAndRenameModelName).join(" & ");
2833
}
2934

30-
if (content.includes(' | ')) {
31-
return content.split(' | ').map(checkAndRenameModelName).join(' | ')
35+
if (content.includes(" | ")) {
36+
return content.split(" | ").map(checkAndRenameModelName).join(" | ");
3237
}
3338

34-
return content
39+
return content;
3540
},
36-
'primitive': content => checkAndRenameModelName(content),
37-
}
41+
primitive: (content) => checkAndRenameModelName(content),
42+
};
3843

3944
const inlineExtraFormatters = {
40-
'object': (parsedSchema) => {
45+
object: (parsedSchema) => {
4146
return {
4247
...parsedSchema,
43-
typeIdentifier: parsedSchema.content.length ? parsedSchema.typeIdentifier : 'type',
44-
content: parsedSchema.content.length ? `{ ${parsedSchema.content.map(part => part.field).join(', ')} }` : 'object'
45-
}
48+
typeIdentifier: parsedSchema.content.length ? parsedSchema.typeIdentifier : "type",
49+
content: parsedSchema.content.length
50+
? `{ ${parsedSchema.content.map((part) => part.field).join(", ")} }`
51+
: "object",
52+
};
4653
},
47-
'enum': (parsedSchema) => {
54+
enum: (parsedSchema) => {
4855
return {
4956
...parsedSchema,
50-
content: _.map(parsedSchema.content, ({ value }) => `${value}`).join(' | ')
51-
}
52-
}
53-
}
54-
57+
content: _.map(parsedSchema.content, ({ value }) => `${value}`).join(" | "),
58+
};
59+
},
60+
};
5561

5662
module.exports = {
5763
formatters,
5864
inlineExtraFormatters,
59-
}
65+
};

0 commit comments

Comments
 (0)