Skip to content

Commit ce92f37

Browse files
added 2 sample configs; adsjuted to only generate examples for application/json
1 parent 6ecd336 commit ce92f37

51 files changed

Lines changed: 5585 additions & 11 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
generatorName: spring
2+
outputDir: samples/server/petstore/springboot-api-response-examples
3+
library: spring-boot
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/api-response-examples_issue17610.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
6+
additionalProperties:
7+
artifactId: springboot-api-response-examples
8+
documentationProvider: springdoc
9+
useSpringBoot3: true
10+
java8: true
11+
delegatePattern: true
12+
useBeanValidation: true
13+
hideGenerationTimestamp: "true"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
generatorName: spring
2+
outputDir: samples/server/petstore/springboot-petstore-with-api-response-examples
3+
library: spring-boot
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore_with_api_response_examples.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
6+
additionalProperties:
7+
artifactId: springboot-petstore-with-api-response-examples
8+
documentationProvider: springdoc
9+
useSpringBoot3: true
10+
java8: true
11+
delegatePattern: true
12+
useBeanValidation: true
13+
hideGenerationTimestamp: "true"

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@ public static Map<String, Example> getExamplesFromResponse(OpenAPI openAPI, ApiR
3434
}
3535

3636
private static Map<String, Example> getExamplesFromContent(Content content) {
37-
if (content == null || content.isEmpty()) {
37+
if (content == null || content.isEmpty())
3838
return Collections.emptyMap();
39+
40+
if (content.containsKey("application/json")) {
41+
Map<String, Example> examples = content.get("application/json").getExamples();
42+
if (content.size() > 1 && examples != null && !examples.isEmpty())
43+
once(LOGGER).warn("More than one content media types found in response. Only response examples of the application/json will be taken for codegen.");
44+
45+
return examples;
3946
}
40-
Map.Entry<String, MediaType> entry = content.entrySet().iterator().next();
41-
if (content.size() > 1) {
42-
once(LOGGER).debug("Multiple API response examples found in the OAS 'content' section, returning only the first one ({})",
43-
entry.getKey());
44-
}
45-
return entry.getValue().getExamples();
47+
48+
once(LOGGER).warn("No application/json content media type found in response. Response examples can only be generated for application/json media type.");
49+
50+
return Collections.emptyMap();
4651
}
4752

4853

@@ -68,10 +73,10 @@ public static List<Map<String, Object>> unaliasExamples(OpenAPI openapi, Map<Str
6873
Object exampleValue;
6974
if(example.getValue().get$ref() != null){
7075
exampleValue = actualComponentsExamples.get(exampleName).getValue();
71-
LOGGER.trace("Unaliased example value from components examples: {}", exampleValue);
76+
LOGGER.debug("Unaliased example value from components examples: {}", exampleValue);
7277
} else {
7378
exampleValue = example.getValue().getValue();
74-
LOGGER.trace("Retrieved example value directly from the api response example definition: {}", exampleValue);
79+
LOGGER.debug("Retrieved example value directly from the api response example definition: {}", exampleValue);
7580
}
7681

7782
exampleRepr.put("exampleName", exampleName);

modules/openapi-generator/src/main/resources/JavaSpring/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public interface {{classname}} {
172172
{{#responses}}
173173
@ApiResponse(responseCode = {{#isDefault}}"default"{{/isDefault}}{{^isDefault}}"{{{code}}}"{{/isDefault}}, description = "{{{message}}}"{{#baseType}}, content = {
174174
{{#produces}}
175-
@Content(mediaType = "{{{mediaType}}}", {{#isArray}}array = @ArraySchema({{/isArray}}schema = @Schema(implementation = {{{baseType}}}.class){{#isArray}}){{/isArray}}{{^examples.0}}{{#-last}}){{/-last}}{{^-last}}),{{/-last}}{{/examples.0}}{{#examples.0}}, examples = {
175+
@Content(mediaType = "{{{mediaType}}}", {{#isArray}}array = @ArraySchema({{/isArray}}schema = @Schema(implementation = {{{baseType}}}.class){{#isArray}}){{/isArray}}{{^isJson}}{{#-last}}){{/-last}}{{^-last}}),{{/-last}}{{/isJson}}{{#isJson}}{{^examples.0}}{{#-last}}){{/-last}}{{^-last}}),{{/-last}}{{/examples.0}}{{#examples.0}}, examples = {
176176
{{#examples}}
177177
@ExampleObject(
178178
name = "{{{exampleName}}}",
@@ -184,7 +184,7 @@ public interface {{classname}} {
184184
{{/-last}}
185185
{{^-last}}
186186
}),
187-
{{/-last}}{{/examples.0}}
187+
{{/-last}}{{/examples.0}}{{/isJson}}
188188
{{/produces}}
189189
}{{/baseType}}){{^-last}},{{/-last}}
190190
{{/responses}}

0 commit comments

Comments
 (0)