forked from loafle/openapi-generator-original
[Enhancement] added support for custom type & format mapping (#9285)
* resolve merge conflicts * use + in type mapping, add tests * add new test spec
This commit is contained in:
parent
a9c7644c32
commit
77dfd40681
@ -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.
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user