diff --git a/__mocks__/api.yaml b/__mocks__/api.yaml index 2c50aa5..80ac33d 100644 --- a/__mocks__/api.yaml +++ b/__mocks__/api.yaml @@ -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 diff --git a/__mocks__/openapi_v3/api.yaml b/__mocks__/openapi_v3/api.yaml index abc4938..3af7a21 100644 --- a/__mocks__/openapi_v3/api.yaml +++ b/__mocks__/openapi_v3/api.yaml @@ -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 diff --git a/package.json b/package.json index 4c216c8..53e13a8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/gen-api-models/__tests__/parse.test.ts b/src/commands/gen-api-models/__tests__/parse.test.ts index 10a0fb9..d36c318 100644 --- a/src/commands/gen-api-models/__tests__/parse.test.ts +++ b/src/commands/gen-api-models/__tests__/parse.test.ts @@ -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, diff --git a/src/commands/gen-api-models/parse.v2.ts b/src/commands/gen-api-models/parse.v2.ts index 4c5e259..f3d4316 100644 --- a/src/commands/gen-api-models/parse.v2.ts +++ b/src/commands/gen-api-models/parse.v2.ts @@ -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: diff --git a/src/commands/gen-api-models/parse.v3.ts b/src/commands/gen-api-models/parse.v3.ts index 75412ec..f67e701 100644 --- a/src/commands/gen-api-models/parse.v3.ts +++ b/src/commands/gen-api-models/parse.v3.ts @@ -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; } diff --git a/yarn.lock b/yarn.lock index f142bd2..ac17fbe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"