forked from loafle/openapi-generator-original
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:
@@ -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;
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user