Skip to content

Commit 9c89e6a

Browse files
authored
Better handling of form data (#2818)
* better warning message, better handling of form payload * fix form data detection * better format
1 parent 250e528 commit 9c89e6a

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ public String toDefaultValue(Schema schema) {
12861286

12871287
/**
12881288
* Return property value depending on property type.
1289+
*
12891290
* @param schema property type
12901291
* @return property value
12911292
*/
@@ -1450,7 +1451,8 @@ private static String getPrimitiveType(Schema schema) {
14501451
} else if (ModelUtils.isDoubleSchema(schema)) {
14511452
return SchemaTypeUtil.DOUBLE_FORMAT;
14521453
} else {
1453-
LOGGER.warn("Unknown `format` detected for " + schema.getName() + ": " + schema.getFormat());
1454+
LOGGER.warn("Unknown `format` {} detected for type `number`. Defaulting to `number`", schema.getFormat());
1455+
return "number";
14541456
}
14551457
} else if (ModelUtils.isIntegerSchema(schema)) {
14561458
if (ModelUtils.isLongSchema(schema)) {
@@ -1891,7 +1893,7 @@ public String getterAndSetterCapitalize(String name) {
18911893
*/
18921894
public CodegenProperty fromProperty(String name, Schema p) {
18931895
if (p == null) {
1894-
LOGGER.error("Undefined property/schema for `{}`. Default to type:string.", name);
1896+
LOGGER.error("Undefined property/schema for `{}`. Default to type:string.", name);
18951897
return null;
18961898
}
18971899
LOGGER.debug("debugging fromProperty for " + name + " : " + p);
@@ -2041,7 +2043,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
20412043
if (innerSchema == null) {
20422044
LOGGER.error("Undefined array inner type for `{}`. Default to String.", p.getName());
20432045
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
2044-
((ArraySchema)p).setItems(innerSchema);
2046+
((ArraySchema) p).setItems(innerSchema);
20452047
}
20462048
} else if (ModelUtils.isMapSchema(p)) {
20472049
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getAdditionalProperties(p));
@@ -2123,7 +2125,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
21232125
if (innerSchema == null) {
21242126
LOGGER.error("Undefined array inner type for `{}`. Default to String.", p.getName());
21252127
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
2126-
((ArraySchema)p).setItems(innerSchema);
2128+
((ArraySchema) p).setItems(innerSchema);
21272129
}
21282130
CodegenProperty cp = fromProperty(itemName, innerSchema);
21292131
updatePropertyForArray(property, cp);
@@ -2515,8 +2517,8 @@ public CodegenOperation fromOperation(String path,
25152517
CodegenParameter bodyParam = null;
25162518
RequestBody requestBody = operation.getRequestBody();
25172519
if (requestBody != null) {
2518-
if ("application/x-www-form-urlencoded".equalsIgnoreCase(getContentType(requestBody)) ||
2519-
"multipart/form-data".equalsIgnoreCase(getContentType(requestBody))) {
2520+
if (getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
2521+
getContentType(requestBody).toLowerCase(Locale.ROOT).startsWith("multipart/form-data")) {
25202522
// process form parameters
25212523
formParams = fromRequestBodyToFormParameters(requestBody, imports);
25222524
for (CodegenParameter cp : formParams) {
@@ -4194,7 +4196,8 @@ public void writePropertyBack(String propertyKey, boolean value) {
41944196

41954197
protected String getContentType(RequestBody requestBody) {
41964198
if (requestBody == null || requestBody.getContent() == null || requestBody.getContent().isEmpty()) {
4197-
return null;
4199+
LOGGER.warn("Cannot determine the content type. Default to UNKNOWN_CONTENT_TYPE.");
4200+
return "UNKNOWN_CONTENT_TYPE";
41984201
}
41994202
return new ArrayList<>(requestBody.getContent().keySet()).get(0);
42004203
}
@@ -4275,7 +4278,9 @@ public boolean hasFormParameter(OpenAPI openAPI, Operation operation) {
42754278
}
42764279

42774280
for (String consume : consumesInfo) {
4278-
if ("application/x-www-form-urlencoded".equalsIgnoreCase(consume) || "multipart/form-data".equalsIgnoreCase(consume)) {
4281+
if (consume != null &&
4282+
consume.toLowerCase(Locale.ROOT).startsWith("application/x-www-form-urlencoded") ||
4283+
consume.toLowerCase(Locale.ROOT).startsWith("multipart/form-data")) {
42794284
return true;
42804285
}
42814286
}
@@ -4849,7 +4854,7 @@ public List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariabl
48494854
}
48504855

48514856
private void setParameterNullable(CodegenParameter parameter, CodegenProperty property) {
4852-
if(parameter == null || property == null) {
4857+
if (parameter == null || property == null) {
48534858
return;
48544859
}
48554860
parameter.isNullable = property.isNullable;
@@ -4898,7 +4903,7 @@ public boolean isEnableMinimalUpdate() {
48984903
/**
48994904
* Set the boolean value indicating the state of the option for updating only changed files
49004905
*
4901-
* @param enableMinimalUpdate true to enable minimal update
4906+
* @param enableMinimalUpdate true to enable minimal update
49024907
*/
49034908
@Override
49044909
public void setEnableMinimalUpdate(boolean enableMinimalUpdate) {

0 commit comments

Comments
 (0)