Support examples in Response Content (#14336)

* Add examples map and overload constructor

* Create CodegenMediaType with examples

* Add test
This commit is contained in:
Beppe Catanese 2023-01-28 05:42:00 +01:00 committed by GitHub
parent 03299528e9
commit 1c157a2847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 1 deletions

View File

@ -1,13 +1,17 @@
package org.openapitools.codegen;
import io.swagger.v3.oas.models.examples.Example;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
public class CodegenMediaType {
private CodegenProperty schema;
private LinkedHashMap<String, CodegenEncoding> encoding;
private HashMap<String, SchemaTestCase> testCases = new HashMap<>();
private Map<String, Example> examples = null;
public CodegenMediaType(CodegenProperty schema, LinkedHashMap<String, CodegenEncoding> encoding, HashMap<String, SchemaTestCase> testCases) {
this.schema = schema;
@ -17,6 +21,11 @@ public class CodegenMediaType {
}
}
public CodegenMediaType(CodegenProperty schema, LinkedHashMap<String, CodegenEncoding> encoding, HashMap<String, SchemaTestCase> testCases, Map<String, Example> examples) {
this(schema, encoding, testCases);
this.examples = examples;
}
public CodegenProperty getSchema() {
return schema;
}
@ -27,6 +36,10 @@ public class CodegenMediaType {
public HashMap<String, SchemaTestCase> getTestCases() { return testCases; }
public Map<String, Example> getExamples() {
return examples;
}
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenMediaType{");
sb.append("schema=").append(schema);

View File

@ -7236,7 +7236,7 @@ public class DefaultCodegen implements CodegenConfig {
}
}
CodegenMediaType codegenMt = new CodegenMediaType(schemaProp, ceMap, schemaTestCases);
CodegenMediaType codegenMt = new CodegenMediaType(schemaProp, ceMap, schemaTestCases, mt.getExamples());
cmtContent.put(contentType, codegenMt);
if (schemaProp != null) {
if (addSchemaImportsFromV3SpecLocations) {

View File

@ -4189,6 +4189,9 @@ public class DefaultCodegenTest {
assertEquals(cp.complexType, "coordinates");
assertEquals(cp.baseName, "SchemaFor201ResponseBodyApplicationJson");
assertNotNull(mt.getExamples());
assertEquals(mt.getExamples().size(), 2);
mt = content.get("text/plain");
assertNull(mt.getEncoding());
cp = mt.getSchema();

View File

@ -44,6 +44,15 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/coordinates'
examples:
Example Amsterdam coordinates:
value:
lat: 52.377956
long: 4.897070
Example San Francisco coordinates:
value:
lat: 37.773972
long: -122.431297
text/plain:
schema:
$ref: '#/components/schemas/stringWithMinLength'
@ -58,6 +67,15 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/coordinates'
examples:
Example Amsterdam coordinates:
value:
lat: 52.377956
long: 4.897070
Example San Francisco coordinates:
value:
lat: 37.773972
long: -122.431297
text/plain:
schema:
$ref: '#/components/schemas/stringWithMinLength'