From d68a186c5e4968e18de3f0076c341aaaf950d57e Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Mon, 30 Sep 2024 02:49:49 +0100 Subject: [PATCH] test(OpenAPINormalizer): nullability normalization for 3.1 specs (#19714) * test(OpenAPINormalizer): nullability normalization for 3.1 specs * chore: samples --- .../codegen/OpenAPINormalizerTest.java | 18 ++++++++++++++++++ .../builds/null-types-simple/.gitattributes | 8 ++++++++ .../null-types-simple/.openapi-generator/FILES | 1 + .../builds/null-types-simple/http/http.ts | 14 ++++++++------ 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 samples/client/others/typescript/builds/null-types-simple/.gitattributes diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java index 174536e8492..70c04397e23 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java @@ -624,6 +624,24 @@ public class OpenAPINormalizerTest { assertEquals(((Schema) schema6.getProperties().get("arrayOfStrings")).getType(), "array"); } + @Test + public void testOpenAPINormalizerProcessingArraySchema31NullabilitySpec() { + OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/null-types-simple.yaml"); + Schema schema = openAPI.getComponents().getSchemas().get("WithNullableType"); + + assertNull(((Schema) schema.getProperties().get("arrayDataOrNull")).getNullable()); + assertNull(((Schema) schema.getProperties().get("stringDataOrNull")).getNullable()); + assertNull(((Schema) schema.getProperties().get("oneofOrNull")).getNullable()); + + Map inputRules = Map.of("NORMALIZE_31SPEC", "true"); + OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules); + openAPINormalizer.normalize(); + + assertTrue(((Schema) schema.getProperties().get("arrayDataOrNull")).getNullable()); + assertTrue(((Schema) schema.getProperties().get("stringDataOrNull")).getNullable()); + assertTrue(((Schema) schema.getProperties().get("oneofOrNull")).getNullable()); + } + @Test public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() { // to test the rule SIMPLIFY_ONEOF_ANYOF with 3.1 spec diff --git a/samples/client/others/typescript/builds/null-types-simple/.gitattributes b/samples/client/others/typescript/builds/null-types-simple/.gitattributes new file mode 100644 index 00000000000..7bf5a17b22f --- /dev/null +++ b/samples/client/others/typescript/builds/null-types-simple/.gitattributes @@ -0,0 +1,8 @@ +**/* linguist-generated +*.md linguist-documentation + +.gitattributes text +.gitattributes export-ignore + +.gitignore text +.gitignore export-ignore diff --git a/samples/client/others/typescript/builds/null-types-simple/.openapi-generator/FILES b/samples/client/others/typescript/builds/null-types-simple/.openapi-generator/FILES index aaf21969459..f586694f5f6 100644 --- a/samples/client/others/typescript/builds/null-types-simple/.openapi-generator/FILES +++ b/samples/client/others/typescript/builds/null-types-simple/.openapi-generator/FILES @@ -1,3 +1,4 @@ +.gitattributes .gitignore README.md apis/baseapi.ts diff --git a/samples/client/others/typescript/builds/null-types-simple/http/http.ts b/samples/client/others/typescript/builds/null-types-simple/http/http.ts index f38a52ec130..b0d0d479e21 100644 --- a/samples/client/others/typescript/builds/null-types-simple/http/http.ts +++ b/samples/client/others/typescript/builds/null-types-simple/http/http.ts @@ -33,6 +33,8 @@ export class HttpException extends Error { */ export type RequestBody = undefined | string | FormData | URLSearchParams; +type Headers = Record; + function ensureAbsoluteUrl(url: string) { if (url.startsWith("http://") || url.startsWith("https://")) { return url; @@ -44,7 +46,7 @@ function ensureAbsoluteUrl(url: string) { * Represents an HTTP request context */ export class RequestContext { - private headers: { [key: string]: string } = {}; + private headers: Headers = {}; private body: RequestBody = undefined; private url: URL; @@ -93,7 +95,7 @@ export class RequestContext { return this.httpMethod; } - public getHeaders(): { [key: string]: string } { + public getHeaders(): Headers { return this.headers; } @@ -160,7 +162,7 @@ export class SelfDecodingBody implements ResponseBody { export class ResponseContext { public constructor( public httpStatusCode: number, - public headers: { [key: string]: string }, + public headers: Headers, public body: ResponseBody ) {} @@ -171,8 +173,8 @@ export class ResponseContext { * Parameter names are converted to lower case * The first parameter is returned with the key `""` */ - public getParsedHeader(headerName: string): { [parameter: string]: string } { - const result: { [parameter: string]: string } = {}; + public getParsedHeader(headerName: string): Headers { + const result: Headers = {}; if (!this.headers[headerName]) { return result; } @@ -245,7 +247,7 @@ export function wrapHttpLibrary(promiseHttpLibrary: PromiseHttpLibrary): HttpLib export class HttpInfo extends ResponseContext { public constructor( public httpStatusCode: number, - public headers: { [key: string]: string }, + public headers: Headers, public body: ResponseBody, public data: T, ) {