Skip to content
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

Add Support for Non Array Schema Parameter Native Types #326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions __mocks__/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,18 @@ definitions:
$ref: "#/definitions/DefinitionFieldWithDash"
required:
- items
AnObjectWithAnItemsNativeTypeField:
description: |-
What if a object has a field named items?
The case is an object like { items: [] }, which is legal
type: object
properties:
items:
type: array
items:
type: string
required:
- items
ObjectDefinitionWithImplicitType:
# Sometimes type isn't specified, nevertheless we can infer the definition to be an object
# type: object
Expand Down
12 changes: 12 additions & 0 deletions __mocks__/openapi_v3/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,18 @@ components:
$ref: "#/components/schemas/DefinitionFieldWithDash"
required:
- items
AnObjectWithAnItemsNativeTypeField:
description: |-
What if a object has a field named items of a native type?
The case is an object like { items: [] }, which is legal
type: object
properties:
items:
type: array
items:
type: string
required:
- items
AnObjectWithRefImport:
title: AnObjectWithRefImport
description: Describes an object with a ref import
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@pagopa/ts-commons": "^10.15.0",
"fs-extra": "^6.0.0",
"nunjucks": "^3.2.3",
"openapi-types": "^10.0.0",
"openapi-types": "^12.1.3",
"prettier": "^1.12.1",
"safe-identifier": "^0.4.2",
"swagger-parser": "^10.0.3",
Expand Down
24 changes: 24 additions & 0 deletions src/commands/gen-api-models/__tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,30 @@ describe.each`
}
});

it("should handle AnObjectWithAnItemsNativeTypeField", async () => {
const definition = getDefinitionOrFail(spec, "AnObjectWithAnItemsNativeTypeField");

const parsed = getParser(spec).parseDefinition(
// @ts-ignore
definition
);

//expect(parsed).toEqual({});
expect(parsed.type).toBe("object");
expect(parsed.properties?.items).toEqual(expect.any(Object));

if (parsed.properties?.items && "type" in parsed.properties?.items) {
expect(parsed.properties?.items.type).toBe("array");
expect(parsed.properties?.items.items).toEqual(
expect.objectContaining({
type: "string"
})
);
} else {
fail("a type should be specified");
}
});

it("should handle ObjectDefinitionWithImplicitType", async () => {
const definition = getDefinitionOrFail(
spec,
Expand Down
4 changes: 4 additions & 0 deletions src/commands/gen-api-models/parse.v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ function specTypeToTs(parameter: OpenAPIV2.ParameterObject): string {
switch (parameter.type) {
case "integer":
return "number";
case "array":
return "type" in parameter.items
? `${parameter.items.type}[]`
: parameter.type;
case "file":
return `{ "uri": string, "name": string, "type": string }`;
default:
Expand Down
2 changes: 2 additions & 0 deletions src/commands/gen-api-models/parse.v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ function specTypeToTs(parameter: OpenAPIV3.ParameterObject): string {
switch (schema.type) {
case "integer":
return "number";
case "array":
return "type" in schema.items ? `${schema.items.type}[]` : schema.type;
default:
return schema.type;
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3986,10 +3986,10 @@ onetime@^5.1.0:
dependencies:
mimic-fn "^2.1.0"

openapi-types@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-10.0.0.tgz#0debbf663b2feed0322030b5b7c9080804076934"
integrity sha512-Y8xOCT2eiKGYDzMW9R4x5cmfc3vGaaI4EL2pwhDmodWw1HlK18YcZ4uJxc7Rdp7/gGzAygzH9SXr6GKYIXbRcQ==
openapi-types@^12.1.3:
version "12.1.3"
resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-12.1.3.tgz#471995eb26c4b97b7bd356aacf7b91b73e777dd3"
integrity sha512-N4YtSYJqghVu4iek2ZUvcN/0aqH1kRDuNqzcycDxhOUpg7GdvLa2F3DgS6yBNhInhv2r/6I0Flkn7CqL8+nIcw==

optionator@^0.8.1:
version "0.8.3"
Expand Down