Skip to content

Commit

Permalink
Update DefaultCodeGen to allow additional primitive types (OpenAPIToo…
Browse files Browse the repository at this point in the history
…ls#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
  • Loading branch information
steco authored and wing328 committed Jul 3, 2019
1 parent 1fc7740 commit b44f6c3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/windows/android-petstore-httpclient.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ If Not Exist %executable% (
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g android -o samples\client\petstore\android\httpclient-Dlibrary=httpclient
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g android -o samples\client\petstore\android\httpclient -Dlibrary=httpclient

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ private String getSingleSchemaType(Schema schema) {
* @param schema
* @return type
*/
private static String getPrimitiveType(Schema schema) {
private String getPrimitiveType(Schema schema) {
if (schema == null) {
throw new RuntimeException("schema cannot be null in getPrimitiveType");
} else if (ModelUtils.isStringSchema(schema) && "number".equals(schema.getFormat())) {
Expand Down Expand Up @@ -1519,6 +1519,13 @@ private static String getPrimitiveType(Schema schema) {
} else if (ModelUtils.isURISchema(schema)) {
return "URI";
} 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";
} else if (ModelUtils.isFreeFormObject(schema)) {
return "object";
Expand Down Expand Up @@ -3668,7 +3675,7 @@ private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Sch
* @param schemas The complete set of model definitions (schemas).
* @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()) {
return new HashMap<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,15 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {

@Test
public void testArraySchemaIsNotIncluedInAliases() throws Exception {
final DefaultCodegen codegen = new DefaultCodegen();
Map<String, Schema> schemas = new HashMap<String, Schema>() {
{
put("ArraySchemaTest", new ArraySchema());
}

};

Map<String, String> aliases = DefaultCodegen.getAllAliases(schemas);
Map<String, String> aliases = codegen.getAllAliases(schemas);

Assert.assertEquals(aliases.size(), 0);
}
Expand Down

0 comments on commit b44f6c3

Please sign in to comment.