Skip to content

Commit

Permalink
Release 1.6.2 (#38)
Browse files Browse the repository at this point in the history
* feat: handle nullable option for primitive types (#37)

feat: handle nullable option for primitive types (#36)

Co-authored-by: svolkov <svolkov@everpoint.ru>

* bump: up version to 1.6.2; chore: update swagger2openapi to latest (6.0.0); docs: update CHANGELOG

Co-authored-by: svolkov <svolkov@everpoint.ru>
  • Loading branch information
js2me and js2me authored Mar 25, 2020
1 parent 10d6e9c commit 2fdcf73
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 119 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# next release

# 1.6.2

Fixes:

- Nullable not included in type definition ([issue](https://github.com/acacode/swagger-typescript-api/issues/36))

Internal:

- Update `swagger2openapi`(`6.0.0`) dependency

# 1.6.1

Internal:
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-typescript-api",
"version": "1.6.1",
"version": "1.6.2",
"description": "Create typescript api module from swagger schema",
"scripts": {
"cli": "node index.js -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
Expand Down Expand Up @@ -36,7 +36,7 @@
"lodash": "^4.17.15",
"mustache": "^4.0.0",
"prettier": "^2.0.2",
"swagger2openapi": "^5.4.0"
"swagger2openapi": "^6.0.0"
},
"bin": {
"swagger-typescript-api": "index.js",
Expand Down
41 changes: 22 additions & 19 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ const typeAliases = {
integer: "number",
};

const findSchemaType = schema => {
const findSchemaType = (schema) => {
if (schema.enum) return "enum";
if (schema.properties) return "object";
if (schema.allOf || schema.oneOf || schema.anyOf || schema.not) return "complex";

return "primitive";
};

const getPrimitiveType = property => {
const type = _.get(property, "type");
return typeAliases[type] || type || DEFAULT_PRIMITIVE_TYPE;
const getPrimitiveType = (property) => {
const { type, nullable } = property || {};
const primitiveType = typeAliases[type] || type;
return primitiveType
? (nullable && `${primitiveType} | null`) || primitiveType
: DEFAULT_PRIMITIVE_TYPE;
};

const specificObjectTypes = {
Expand All @@ -31,24 +34,24 @@ const specificObjectTypes = {
},
};

const getRefType = property => {
const getRefType = (property) => {
const ref = property && property["$ref"];
return (ref && config.componentsMap[ref]) || null;
};

const getRefTypeName = property => {
const getRefTypeName = (property) => {
const refTypeInfo = getRefType(property);
return refTypeInfo && checkAndRenameModelName(refTypeInfo.typeName);
};

const getType = property => {
const getType = (property) => {
if (!property) return DEFAULT_PRIMITIVE_TYPE;

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

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

const complexSchemaParsers = {
oneOf: schema => {
oneOf: (schema) => {
// T1 | T2
const combined = _.map(schema.oneOf, complexTypeGetter);
return combined.join(" | ");
},
allOf: schema => {
allOf: (schema) => {
// T1 & T2
return _.map(schema.allOf, complexTypeGetter).join(" & ");
},
anyOf: schema => {
anyOf: (schema) => {
// T1 | T2 | (T1 & T2)
const combined = _.map(schema.anyOf, complexTypeGetter);
return `${combined.join(" | ")}` + (combined.length > 1 ? ` | (${combined.join(" & ")})` : "");
},
// TODO
not: schema => {
not: (schema) => {
// TODO
},
};

const getComplexType = schema => {
const getComplexType = (schema) => {
if (schema.oneOf) return "oneOf";
if (schema.allOf) return "allOf";
if (schema.anyOf) return "anyOf";
Expand All @@ -108,7 +111,7 @@ const schemaParsers = {
typeIdentifier: isIntegerEnum ? "type" : "enum",
name: typeName,
description: formatDescription(schema.description),
content: _.map(schema.enum, key => ({
content: _.map(schema.enum, (key) => ({
key,
type,
value: isIntegerEnum ? `${key}` : `"${key}"`,
Expand All @@ -117,7 +120,7 @@ const schemaParsers = {
},
object: (schema, typeName) => {
if (_.isArray(schema.required) && schema.properties) {
schema.required.forEach(requiredFieldName => {
schema.required.forEach((requiredFieldName) => {
if (schema.properties[requiredFieldName]) {
schema.properties[requiredFieldName].required = true;
}
Expand All @@ -134,7 +137,7 @@ const schemaParsers = {
typeIdentifier: "interface",
name: typeName,
description: formatDescription(schema.description),
allFieldsAreOptional: !_.some(_.values(typeContent), part => part.isRequired),
allFieldsAreOptional: !_.some(_.values(typeContent), (part) => part.isRequired),
content: typeContent,
};
},
Expand Down Expand Up @@ -172,7 +175,7 @@ const schemaParsers = {
},
};

const checkAndFixSchema = schema => {
const checkAndFixSchema = (schema) => {
if (schema.items && !schema.type) {
schema.type = "array";
}
Expand Down Expand Up @@ -208,10 +211,10 @@ const parseSchema = (rawSchema, typeName, formattersMap) => {
);
};

const parseSchemas = components =>
const parseSchemas = (components) =>
_.map(_.get(components, "schemas"), (schema, typeName) => parseSchema(schema, typeName));

const getInlineParseContent = rawTypeData =>
const getInlineParseContent = (rawTypeData) =>
parseSchema(rawTypeData, null, inlineExtraFormatters).content;

module.exports = {
Expand Down
86 changes: 46 additions & 40 deletions src/typeFormatters.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,65 @@
const _ = require('lodash');
const _ = require("lodash");
const { checkAndRenameModelName } = require("./modelNames");

const formatters = {
'enum': content => _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(',\n'),
'intEnum': content => _.map(content, ({ value }) => value).join(' | '),
'object': content => _.map(content, part => {
const extraSpace = ' '
const result = `${extraSpace}${part.field};\n`;

const comments = [
part.title,
part.description
].filter(Boolean)

const commonText = comments.length ? [
'',
'/**',
...comments.reduce((acc, comment) => [...acc, ...comment.split(/\n/g).map(part => ` * ${part}`)], []),
' */'
].map(part => `${extraSpace}${part}\n`).join('') : '';

return `${commonText}${result}`;
}).join(''),
'type': content => {
if (content.includes(' & ')) {
return content.split(' & ').map(checkAndRenameModelName).join(' & ')
enum: (content) => _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(",\n"),
intEnum: (content) => _.map(content, ({ value }) => value).join(" | "),
object: (content) =>
_.map(content, (part) => {
const extraSpace = " ";
const result = `${extraSpace}${part.field};\n`;

const comments = [part.title, part.description].filter(Boolean);

const commonText = comments.length
? [
"",
"/**",
...comments.reduce(
(acc, comment) => [...acc, ...comment.split(/\n/g).map((part) => ` * ${part}`)],
[],
),
" */",
]
.map((part) => `${extraSpace}${part}\n`)
.join("")
: "";

return `${commonText}${result}`;
}).join(""),
type: (content) => {
if (content.includes(" & ")) {
return content.split(" & ").map(checkAndRenameModelName).join(" & ");
}

if (content.includes(' | ')) {
return content.split(' | ').map(checkAndRenameModelName).join(' | ')
if (content.includes(" | ")) {
return content.split(" | ").map(checkAndRenameModelName).join(" | ");
}

return content
return content;
},
'primitive': content => checkAndRenameModelName(content),
}
primitive: (content) => checkAndRenameModelName(content),
};

const inlineExtraFormatters = {
'object': (parsedSchema) => {
object: (parsedSchema) => {
return {
...parsedSchema,
typeIdentifier: parsedSchema.content.length ? parsedSchema.typeIdentifier : 'type',
content: parsedSchema.content.length ? `{ ${parsedSchema.content.map(part => part.field).join(', ')} }` : 'object'
}
typeIdentifier: parsedSchema.content.length ? parsedSchema.typeIdentifier : "type",
content: parsedSchema.content.length
? `{ ${parsedSchema.content.map((part) => part.field).join(", ")} }`
: "object",
};
},
'enum': (parsedSchema) => {
enum: (parsedSchema) => {
return {
...parsedSchema,
content: _.map(parsedSchema.content, ({ value }) => `${value}`).join(' | ')
}
}
}

content: _.map(parsedSchema.content, ({ value }) => `${value}`).join(" | "),
};
},
};

module.exports = {
formatters,
inlineExtraFormatters,
}
};
Loading

0 comments on commit 2fdcf73

Please sign in to comment.