Skip to content

Improve array and object handling #226

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
Apr 27, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@manifoldco/swagger-to-ts",
"description": "Generate TypeScript types from Swagger OpenAPI specs",
"main": "dist/cjs",
"version": "2.0.0-alpha.5",
"version": "2.0.0-alpha.6",
"engines": {
"node": ">= 10.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/types/OpenAPI2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ export interface OpenAPI2SchemaObject {
properties?: { [index: string]: OpenAPI2SchemaObject | OpenAPI2Reference };
required?: string[];
title?: string;
type?: OpenAPI2Type;
type?: OpenAPI2Type; // allow this to be optional to cover cases when this is missing
[key: string]: any; // allow arbitrary x-something properties
}
2 changes: 1 addition & 1 deletion src/types/OpenAPI3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ export interface OpenAPI3SchemaObject {
properties?: { [key: string]: OpenAPI3SchemaObject | OpenAPI3Reference };
required?: string[];
title?: string;
type: OpenAPI3Type;
type?: OpenAPI3Type; // allow this to be optional to cover cases when this is missing
[key: string]: any; // allow arbitrary x-something properties
}
16 changes: 3 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,13 @@ export function nodeType(obj: any): SchemaObjectType | undefined {
return "oneOf";
}

// object
if (
obj.type === "object" ||
!!obj.properties ||
!!obj.allOf ||
!!obj.additionalProperties
) {
return "object";
}

// array
if (obj.type === "array") {
if (obj.type === "array" || obj.items) {
return "array";
}

// other / unknown
return obj.type;
// return object by default
return "object";
}

/** Return OpenAPI version from definition */
Expand Down
6 changes: 6 additions & 0 deletions tests/v2/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ describe("transformation", () => {
type: "object",
},
object_unknown: { type: "object" },
object_empty: {},
},
};
expect(swaggerToTS(schema)).toBe(
Expand All @@ -172,6 +173,7 @@ describe("transformation", () => {
};
object_ref: { number?: number };
object_unknown: { [key: string]: any };
object_empty: { [key: string]: any };
}`)
);
});
Expand All @@ -192,6 +194,9 @@ describe("transformation", () => {
},
type: "object",
},
inferred_array: {
items: { $ref: "#/definitions/array" },
},
string: { type: "string" },
array_ref: { items: { $ref: "#/definitions/array" }, type: "array" },
},
Expand All @@ -206,6 +211,7 @@ describe("transformation", () => {
numbers?: number[];
refs?: definitions['string'][];
};
inferred_array: definitions['array'][];
string: string;
array_ref: definitions['array'][];
}`)
Expand Down
33 changes: 31 additions & 2 deletions tests/v3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ describe("types", () => {
type: "object",
},
object_unknown: { type: "object" },
object_empty: {},
},
},
};
Expand All @@ -154,6 +155,7 @@ describe("types", () => {
};
object_ref: { number?: number };
object_unknown: { [key: string]: any };
object_empty: { [key: string]: any };
}
}`)
);
Expand All @@ -179,6 +181,9 @@ describe("types", () => {
},
type: "object",
},
inferred_array: {
items: { $ref: "#/components/schemas/array" },
},
string: { type: "string" },
array_ref: {
items: { $ref: "#/components/schemas/array" },
Expand All @@ -198,6 +203,7 @@ describe("types", () => {
numbers?: number[];
refs?: components['schemas']['string'][];
};
inferred_array: components['schemas']['array'][];
string: string;
array_ref: components['schemas']['array'][];
}
Expand Down Expand Up @@ -377,7 +383,7 @@ describe("OpenAPI3 features", () => {
oneOf: [
{ type: "string" },
{ type: "number" },
{ $ref: "#/components/one_of_ref" },
{ $ref: "#/components/schemas/one_of_ref" },
],
},
},
Expand All @@ -389,6 +395,26 @@ describe("OpenAPI3 features", () => {
},
type: "object",
},
one_of_inferred: {
oneOf: [
{
properties: {
kibana: {
type: "object",
properties: { versions: { type: "string" } },
},
},
},
{
properties: {
elasticsearch: {
type: "object",
properties: { versions: { type: "string" } },
},
},
},
],
},
},
},
};
Expand All @@ -397,8 +423,11 @@ describe("OpenAPI3 features", () => {
format(`
export interface components {
schemas: {
one_of: { options?: string | number | components['one_of_ref'] };
one_of: { options?: string | number | components['schemas']['one_of_ref'] };
one_of_ref: { boolean?: boolean };
one_of_inferred:
| { kibana?: { versions?: string } }
| { elasticsearch?: { versions?: string } }
}
}`)
);
Expand Down