Skip to content

Commit e4f80dc

Browse files
authored
various fix for free-form object (#1751)
1 parent 120c0a0 commit e4f80dc

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,17 +1418,19 @@ public String getSchemaType(Schema schema) {
14181418
}
14191419
}
14201420

1421-
if (StringUtils.isNotBlank(schema.get$ref())) { // reference to another definition/schema
1421+
Schema unaliasSchema = ModelUtils.unaliasSchema(globalSchemas, schema);
1422+
1423+
if (StringUtils.isNotBlank(unaliasSchema.get$ref())) { // reference to another definition/schema
14221424
// get the schema/model name from $ref
1423-
String schemaName = ModelUtils.getSimpleRef(schema.get$ref());
1425+
String schemaName = ModelUtils.getSimpleRef(unaliasSchema.get$ref());
14241426
if (StringUtils.isNotEmpty(schemaName)) {
14251427
return getAlias(schemaName);
14261428
} else {
1427-
LOGGER.warn("Error obtaining the datatype from ref:" + schema.get$ref() + ". Default to 'object'");
1429+
LOGGER.warn("Error obtaining the datatype from ref:" + unaliasSchema.get$ref() + ". Default to 'object'");
14281430
return "object";
14291431
}
14301432
} else { // primitive type or model
1431-
return getAlias(getPrimitiveType(schema));
1433+
return getAlias(getPrimitiveType(unaliasSchema));
14321434
}
14331435
}
14341436

@@ -2115,6 +2117,8 @@ public CodegenProperty fromProperty(String name, Schema p) {
21152117
allowableValues.put("values", _enum);
21162118
property.allowableValues = allowableValues;
21172119
}
2120+
} else if (ModelUtils.isFreeFormObject(p)){
2121+
property.isFreeFormObject = true;
21182122
}
21192123

21202124
property.dataType = getTypeDeclaration(p);
@@ -2918,7 +2922,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
29182922
}
29192923

29202924
if (parameter.getSchema() != null) {
2921-
Schema parameterSchema = parameter.getSchema();
2925+
Schema parameterSchema = ModelUtils.unaliasSchema(globalSchemas, parameter.getSchema());
29222926
if (parameterSchema == null) {
29232927
LOGGER.warn("warning! Schema not found for parameter \"" + parameter.getName() + "\", using String");
29242928
parameterSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to missing type definition.");
@@ -2992,7 +2996,6 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
29922996
// set boolean flag (e.g. isString)
29932997
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
29942998

2995-
29962999
String parameterDataType = this.getParameterDataType(parameter, parameterSchema);
29973000
if (parameterDataType != null) {
29983001
codegenParameter.dataType = parameterDataType;
@@ -4635,6 +4638,25 @@ public CodegenParameter fromRequestBody(RequestBody
46354638
codegenProperty = codegenProperty.items;
46364639
}
46374640

4641+
} else if (ModelUtils.isFreeFormObject(schema)) {
4642+
// HTTP request body is free form object
4643+
CodegenProperty codegenProperty = fromProperty("FREE_FORM_REQUEST_BODY", schema);
4644+
if (codegenProperty != null) {
4645+
if (StringUtils.isEmpty(bodyParameterName)) {
4646+
codegenParameter.baseName = "body"; // default to body
4647+
} else {
4648+
codegenParameter.baseName = bodyParameterName;
4649+
}
4650+
codegenParameter.isPrimitiveType = true;
4651+
codegenParameter.baseType = codegenProperty.baseType;
4652+
codegenParameter.dataType = codegenProperty.dataType;
4653+
codegenParameter.description = codegenProperty.description;
4654+
codegenParameter.paramName = toParamName(codegenParameter.baseName);
4655+
}
4656+
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
4657+
// set nullable
4658+
setParameterNullable(codegenParameter, codegenProperty);
4659+
46384660
} else if (ModelUtils.isObjectSchema(schema) || ModelUtils.isComposedSchema(schema)) {
46394661
CodegenModel codegenModel = null;
46404662
if (StringUtils.isNotBlank(name)) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,16 @@ private Model getParent(Model model) {
430430

431431
Schema schema = schemas.get(name);
432432

433-
// check to see if it's a "map" model
434-
if (ModelUtils.isMapSchema(schema)) {
433+
if (ModelUtils.isFreeFormObject(schema)) { // check to see if it'a a free-form object
434+
LOGGER.info("Model " + name + " not generated since it's a free-form object");
435+
continue;
436+
} else if (ModelUtils.isMapSchema(schema)) { // check to see if it's a "map" model
435437
if (schema.getProperties() == null || schema.getProperties().isEmpty()) {
436438
// schema without property, i.e. alias to map
437439
LOGGER.info("Model " + name + " not generated since it's an alias to map (without property)");
438440
continue;
439441
}
440-
}
441-
442-
// check to see if it's an "array" model
443-
if (ModelUtils.isArraySchema(schema)) {
442+
} else if (ModelUtils.isArraySchema(schema)) { // check to see if it's an "array" model
444443
if (schema.getProperties() == null || schema.getProperties().isEmpty()) {
445444
// schema without property, i.e. alias to array
446445
LOGGER.info("Model " + name + " not generated since it's an alias to array (without property)");
@@ -898,7 +897,7 @@ public List<File> generate() {
898897
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
899898
generateSupportingFiles(files, bundle);
900899
config.processOpenAPI(openAPI);
901-
900+
902901
// reset GeneratorProperties, so that the running thread can be reused for another generator-run
903902
GeneratorProperties.reset();
904903

0 commit comments

Comments
 (0)