Skip to content

Release 3.1.1 #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# next release

# 3.1.1

Fixes:
- `name.includes is not a function` (issue #98)

# 3.1.0

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-typescript-api",
"version": "3.1.0",
"version": "3.1.1",
"description": "Create typescript api module from swagger schema",
"scripts": {
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/modelNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const isValidName = (name) => /^([A-Za-z$_]{1,})$/g.test(name);
const formattedModelNamesMap = new Map();

const checkAndRenameModelName = (name) => {
if (typeof name !== "string") {
console.warn("🔨 wrong name of the model name", name);

return name;
}

if (formattedModelNamesMap.has(name)) {
return formattedModelNamesMap.get(name);
}
Expand Down
22 changes: 21 additions & 1 deletion src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,29 @@ const jsPrimitiveTypes = _.uniq(["number", "string", "boolean"]);
const jsEmptyTypes = _.uniq(["null", "undefined"]);
const formDataTypes = _.uniq([types.file, types.string.binary]);

const stealTypeFromSchema = (rawSchema) => {
const schema = rawSchema || {};

if (schema.type) {
return schema.type;
}
if (schema.enum) {
const enumFieldType = typeof schema.enum[0];
if (enumFieldType === "undefined") return;

return enumFieldType;
}
if (_.keys(schema.properties).length) {
return "object";
}
if (!!schema.items) {
return "array";
}
};

const getTypeAlias = (rawSchema) => {
const schema = rawSchema || {};
const type = toInternalCase(schema.type);
const type = toInternalCase(stealTypeFromSchema(schema));
const format = toInternalCase(schema.format);
const typeAlias = _.get(types, [type, format]) || _.get(types, [type, "$default"]) || types[type];

Expand Down
2 changes: 2 additions & 0 deletions tests/generated/v2.0/another-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export enum PetNames {

export type PetIds = 10 | 20 | 30 | 40;

export type PetIdsWithWrongEnum = 10 | 20 | 30 | 40;

/**
* A pet for sale in the pet store
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/schemas/v2.0/another-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,9 @@
"type": "integer",
"enum": [10, 20, 30, 40]
},
"PetIdsWithWrongEnum": {
"enum": [10, 20, 30, 40]
},
"Pet": {
"type": "object",
"required": ["name", "photoUrls"],
Expand Down