Update DefaultCodeGen to allow additional primitive types (#2799)

* Update DefaultCodeGen to allow additional primitive types

If a string field is specified with a format which is also defined using
--typeMappings, it will be treated as a primitive type

* Fixed typo in android-petstore-httpclient.bat
This commit is contained in:
steco 2019-07-03 02:12:38 +01:00 committed by William Cheng
parent 1fc7740f2e
commit b44f6c302a
3 changed files with 12 additions and 4 deletions

View File

@ -1475,7 +1475,7 @@ public class DefaultCodegen implements CodegenConfig {
* @param schema * @param schema
* @return type * @return type
*/ */
private static String getPrimitiveType(Schema schema) { private String getPrimitiveType(Schema schema) {
if (schema == null) { if (schema == null) {
throw new RuntimeException("schema cannot be null in getPrimitiveType"); throw new RuntimeException("schema cannot be null in getPrimitiveType");
} else if (ModelUtils.isStringSchema(schema) && "number".equals(schema.getFormat())) { } else if (ModelUtils.isStringSchema(schema) && "number".equals(schema.getFormat())) {
@ -1519,6 +1519,13 @@ public class DefaultCodegen implements CodegenConfig {
} else if (ModelUtils.isURISchema(schema)) { } else if (ModelUtils.isURISchema(schema)) {
return "URI"; return "URI";
} else if (ModelUtils.isStringSchema(schema)) { } else if (ModelUtils.isStringSchema(schema)) {
if(typeMapping.containsKey(schema.getFormat())) {
// If the format matches a typeMapping (supplied with the --typeMappings flag)
// then treat the format as a primitive type.
// This allows the typeMapping flag to add a new custom type which can then
// be used in the format field.
return schema.getFormat();
}
return "string"; return "string";
} else if (ModelUtils.isFreeFormObject(schema)) { } else if (ModelUtils.isFreeFormObject(schema)) {
return "object"; return "object";
@ -3668,7 +3675,7 @@ public class DefaultCodegen implements CodegenConfig {
* @param schemas The complete set of model definitions (schemas). * @param schemas The complete set of model definitions (schemas).
* @return A mapping from model name to type alias * @return A mapping from model name to type alias
*/ */
static Map<String, String> getAllAliases(Map<String, Schema> schemas) { Map<String, String> getAllAliases(Map<String, Schema> schemas) {
if (schemas == null || schemas.isEmpty()) { if (schemas == null || schemas.isEmpty()) {
return new HashMap<>(); return new HashMap<>();
} }

View File

@ -189,6 +189,7 @@ public class DefaultCodegenTest {
@Test @Test
public void testArraySchemaIsNotIncluedInAliases() throws Exception { public void testArraySchemaIsNotIncluedInAliases() throws Exception {
final DefaultCodegen codegen = new DefaultCodegen();
Map<String, Schema> schemas = new HashMap<String, Schema>() { Map<String, Schema> schemas = new HashMap<String, Schema>() {
{ {
put("ArraySchemaTest", new ArraySchema()); put("ArraySchemaTest", new ArraySchema());
@ -196,7 +197,7 @@ public class DefaultCodegenTest {
}; };
Map<String, String> aliases = DefaultCodegen.getAllAliases(schemas); Map<String, String> aliases = codegen.getAllAliases(schemas);
Assert.assertEquals(aliases.size(), 0); Assert.assertEquals(aliases.size(), 0);
} }