From 77dfd40681d19681435a5b5a6df42aaebd0e2e65 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 17 Apr 2021 18:24:59 +0800 Subject: [PATCH] [Enhancement] added support for custom type & format mapping (#9285) * resolve merge conflicts * use + in type mapping, add tests * add new test spec --- .../openapitools/codegen/DefaultCodegen.java | 4 ++ .../codegen/ruby/RubyClientCodegenTest.java | 25 ++++++++++++ .../test/resources/3_0/type_mapping_test.yaml | 38 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 modules/openapi-generator/src/test/resources/3_0/type_mapping_test.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 4993420c5e3..38345e24fe0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -2043,6 +2043,10 @@ public class DefaultCodegen implements CodegenConfig { private String getPrimitiveType(Schema schema) { if (schema == null) { throw new RuntimeException("schema cannot be null in getPrimitiveType"); + } else if (typeMapping.containsKey(schema.getType() + "+" + schema.getFormat())) { + // allows custom type_format mapping. + // use {type}+{format} + return typeMapping.get(schema.getType() + "+" + schema.getFormat()); } else if (ModelUtils.isNullType(schema)) { // The 'null' type is allowed in OAS 3.1 and above. It is not supported by OAS 3.0.x, // though this tooling supports it. diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java index 3981f3f6078..f50f20ac135 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java @@ -677,4 +677,29 @@ public class RubyClientCodegenTest { // pattern_dont_escape_backslash '/^pattern\d{3}$/i' NOTE: the double \ is to escape \ in string but is read as single \ Assert.assertEquals(op.allParams.get(2).pattern, "/^pattern\\d{3}$/i"); } + + /** + * We want to make sure that the type mapping works as expect + */ + @Test(description = "test type mapping to handle special format, e.g. string+special") + public void typeMappingTest() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/type_mapping_test.yaml"); + final RubyClientCodegen codegen = new RubyClientCodegen(); + codegen.typeMapping().put("string+special", "VerySpecialStringInRuby"); + + codegen.setOpenAPI(openAPI); + final String path = "/animals"; + final Operation p = openAPI.getPaths().get(path).getGet(); + final CodegenOperation op = codegen.fromOperation(path, "get", p, null); + Assert.assertEquals(op.allParams.get(0).dataType, "VerySpecialStringInRuby"); + + final Schema schema = openAPI.getComponents().getSchemas().get("Animal"); + codegen.setOpenAPI(openAPI); + CodegenModel animal = codegen.fromModel("Animal", schema); + Assert.assertNotNull(animal); + CodegenProperty cp2 = animal.getVars().get(2); + Assert.assertEquals(cp2.name, "mapping_test"); + Assert.assertFalse(cp2.required); + Assert.assertEquals(cp2.dataType, "VerySpecialStringInRuby"); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/type_mapping_test.yaml b/modules/openapi-generator/src/test/resources/3_0/type_mapping_test.yaml new file mode 100644 index 00000000000..4954771e894 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/type_mapping_test.yaml @@ -0,0 +1,38 @@ +openapi: 3.0.0 +info: + title: Sample API to test type mapping + description: API description in Markdown. + version: 1.0.0 +paths: + /animals: + get: + summary: Returns all animals. + description: Optional extended description in Markdown. + parameters: + - in: query + name: parameter_type_mapping + schema: + type: string + format: special + responses: + 200: + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Animal' +components: + schemas: + Animal: + type: object + required: + - className + properties: + className: + type: string + color: + type: string + default: red + mapping_test: + type: string + format: special