Skip examples on complex body in Java (#8887)

* Skip examples on complex body in Java

This removes complex examples in Java which would not result in valid
code.

* Warn example being skip
This commit is contained in:
Thomas Hervé
2021-03-16 03:09:03 +01:00
committed by GitHub
parent fcab51322e
commit a1392305f1
4 changed files with 67 additions and 0 deletions

View File

@@ -23,10 +23,13 @@ import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.io.FilenameUtils;
@@ -984,6 +987,49 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
setParameterExampleValue(codegenParameter);
}
/**
* Return the example value of the parameter. Overrides the parent method in DefaultCodegen
* to not set examples on complex models, as they don't compile properly.
*
* @param codegenParameter Codegen parameter
* @param requestBody Request body
*/
@Override
public void setParameterExampleValue(CodegenParameter codegenParameter, RequestBody requestBody) {
Boolean isModel = (codegenParameter.isModel || (codegenParameter.isContainer && codegenParameter.getItems().isModel));
Content content = requestBody.getContent();
if (content.size() > 1) {
// @see ModelUtils.getSchemaFromContent()
LOGGER.warn("Multiple MediaTypes found, using only the first one");
}
MediaType mediaType = content.values().iterator().next();
if (mediaType.getExample() != null) {
if (isModel) {
LOGGER.warn("Ignoring complex example on request body");
} else {
codegenParameter.example = mediaType.getExample().toString();
return;
}
}
if (mediaType.getExamples() != null && !mediaType.getExamples().isEmpty()) {
Example example = mediaType.getExamples().values().iterator().next();
if (example.getValue() != null) {
if (isModel) {
LOGGER.warn("Ignoring complex example on request body");
} else {
codegenParameter.example = example.getValue().toString();
return;
}
}
}
setParameterExampleValue(codegenParameter);
}
@Override
public void setParameterExampleValue(CodegenParameter p) {
String example;

View File

@@ -1146,6 +1146,13 @@ components:
type: array
items:
$ref: '#/components/schemas/User'
examples:
simple-list:
summary: Simple list example
description: Should not get into code examples
value:
- username: foo
- username: bar
description: List of user object
required: true
Client:

View File

@@ -1257,6 +1257,13 @@ components:
UserArray:
content:
application/json:
examples:
simple-list:
description: Should not get into code examples
summary: Simple list example
value:
- username: foo
- username: bar
schema:
items:
$ref: '#/components/schemas/User'

View File

@@ -1257,6 +1257,13 @@ components:
UserArray:
content:
application/json:
examples:
simple-list:
description: Should not get into code examples
summary: Simple list example
value:
- username: foo
- username: bar
schema:
items:
$ref: '#/components/schemas/User'