Skip to content

Commit cee5f75

Browse files
authored
Feat adds content and header properties to CodegenResponse (#11046)
* Adds responseHeaders to codegenResponse * Sets response headers in codegenResponse * Samples updated * Adds test of response headers * Adds content to CodegenResponse * Sets codegenResponse content * Tests added, test content-data.yaml spec update * Adds mediaTypeSchemaSuffix input to getContent * Tests updated * Updates how response content schema names are set * Adds missing Locale to String.format invocations
1 parent c94d2b2 commit cee5f75

File tree

143 files changed

+365
-86
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+365
-86
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
public class CodegenResponse implements IJsonSchemaValidationProperties {
2323
public final List<CodegenProperty> headers = new ArrayList<CodegenProperty>();
24+
private List<CodegenParameter> responseHeaders = new ArrayList<CodegenParameter>();
2425
public String code;
2526
public boolean is1xx;
2627
public boolean is2xx;
@@ -87,6 +88,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
8788
private boolean hasDiscriminatorWithNonEmptyMapping;
8889
private CodegenComposedSchemas composedSchemas;
8990
private boolean hasMultipleTypes = false;
91+
private LinkedHashMap<String, CodegenMediaType> content;
9092

9193
@Override
9294
public int hashCode() {
@@ -98,7 +100,7 @@ public int hashCode() {
98100
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
99101
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(),
100102
is1xx, is2xx, is3xx, is4xx, is5xx, additionalPropertiesIsAnyType, hasVars, hasRequired,
101-
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes);
103+
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, responseHeaders, content);
102104
}
103105

104106
@Override
@@ -147,6 +149,8 @@ public boolean equals(Object o) {
147149
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
148150
getHasVars() == that.getHasVars() &&
149151
getHasRequired() == that.getHasRequired() &&
152+
Objects.equals(content, that.getContent()) &&
153+
Objects.equals(responseHeaders, that.getResponseHeaders()) &&
150154
Objects.equals(composedSchemas, that.getComposedSchemas()) &&
151155
Objects.equals(vars, that.vars) &&
152156
Objects.equals(requiredVars, that.requiredVars) &&
@@ -176,6 +180,22 @@ public boolean equals(Object o) {
176180

177181
}
178182

183+
public LinkedHashMap<String, CodegenMediaType> getContent() {
184+
return content;
185+
}
186+
187+
public void setContent(LinkedHashMap<String, CodegenMediaType> content) {
188+
this.content = content;
189+
}
190+
191+
public List<CodegenParameter> getResponseHeaders() {
192+
return responseHeaders;
193+
}
194+
195+
public void setResponseHeaders(List<CodegenParameter> responseHeaders) {
196+
this.responseHeaders = responseHeaders;
197+
}
198+
179199
@Override
180200
public String getPattern() {
181201
return pattern;
@@ -488,6 +508,8 @@ public String toString() {
488508
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
489509
sb.append(", composedSchemas=").append(composedSchemas);
490510
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
511+
sb.append(", responseHeaders=").append(responseHeaders);
512+
sb.append(", content=").append(content);
491513
sb.append('}');
492514
return sb.toString();
493515
}

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

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3959,6 +3959,20 @@ public CodegenOperation fromOperation(String path,
39593959
ApiResponse response = operationGetResponsesEntry.getValue();
39603960
addProducesInfo(response, op);
39613961
CodegenResponse r = fromResponse(key, response);
3962+
Map<String, Header> headers = response.getHeaders();
3963+
if (headers != null) {
3964+
List<CodegenParameter> responseHeaders = new ArrayList<>();
3965+
for (Entry<String, Header> entry: headers.entrySet()) {
3966+
String headerName = entry.getKey();
3967+
Header header = entry.getValue();
3968+
CodegenParameter responseHeader = heeaderToCodegenParameter(header, headerName, imports, String.format(Locale.ROOT, "%sResponseParameter", r.code));
3969+
responseHeaders.add(responseHeader);
3970+
}
3971+
r.setResponseHeaders(responseHeaders);
3972+
}
3973+
String mediaTypeSchemaSuffix = String.format(Locale.ROOT, "%sResponseBody", r.code);
3974+
r.setContent(getContent(response.getContent(), imports, mediaTypeSchemaSuffix));
3975+
39623976
if (r.baseType != null &&
39633977
!defaultIncludes.contains(r.baseType) &&
39643978
!languageSpecificPrimitives.contains(r.baseType)) {
@@ -4065,6 +4079,7 @@ public CodegenOperation fromOperation(String path,
40654079
param = ModelUtils.getReferencedParameter(this.openAPI, param);
40664080

40674081
CodegenParameter p = fromParameter(param, imports);
4082+
p.setContent(getContent(param.getContent(), imports, "RequestParameter" + toModelName(param.getName())));
40684083

40694084
// ensure unique params
40704085
if (ensureUniqueParams) {
@@ -4502,7 +4517,6 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
45024517
codegenParameter.isDeprecated = parameter.getDeprecated();
45034518
}
45044519
codegenParameter.jsonSchema = Json.pretty(parameter);
4505-
codegenParameter.setContent(getContent(parameter.getContent(), imports));
45064520

45074521
if (GlobalSettings.getProperty("debugParser") != null) {
45084522
LOGGER.info("working on Parameter {}", parameter.getName());
@@ -6586,11 +6600,36 @@ protected void updateRequestBodyForString(CodegenParameter codegenParameter, Sch
65866600
codegenParameter.pattern = toRegularExpression(schema.getPattern());
65876601
}
65886602

6589-
protected String toMediaTypeSchemaName(String contentType) {
6590-
return toModelName(contentType + "Schema");
6603+
protected String toMediaTypeSchemaName(String contentType, String mediaTypeSchemaSuffix) {
6604+
return "SchemaFor" + mediaTypeSchemaSuffix + toModelName(contentType);
65916605
}
65926606

6593-
protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Set<String> imports) {
6607+
private CodegenParameter heeaderToCodegenParameter(Header header, String headerName, Set<String> imports, String mediaTypeSchemaSuffix) {
6608+
if (header == null) {
6609+
return null;
6610+
}
6611+
Parameter headerParam = new Parameter();
6612+
headerParam.setName(headerName);
6613+
headerParam.setIn("header");
6614+
headerParam.setDescription(header.getDescription());
6615+
headerParam.setRequired(header.getRequired());
6616+
headerParam.setDeprecated(header.getDeprecated());
6617+
Header.StyleEnum style = header.getStyle();
6618+
if (style != null) {
6619+
headerParam.setStyle(Parameter.StyleEnum.valueOf(style.name()));
6620+
}
6621+
headerParam.setExplode(header.getExplode());
6622+
headerParam.setSchema(header.getSchema());
6623+
headerParam.setExamples(header.getExamples());
6624+
headerParam.setExample(header.getExample());
6625+
headerParam.setContent(header.getContent());
6626+
headerParam.setExtensions(header.getExtensions());
6627+
CodegenParameter param = fromParameter(headerParam, imports);
6628+
param.setContent(getContent(headerParam.getContent(), imports, mediaTypeSchemaSuffix));
6629+
return param;
6630+
}
6631+
6632+
protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Set<String> imports, String mediaTypeSchemaSuffix) {
65946633
if (content == null) {
65956634
return null;
65966635
}
@@ -6609,20 +6648,7 @@ protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Se
66096648
for (Entry<String, Header> headerEntry: encHeaders.entrySet()) {
66106649
String headerName = headerEntry.getKey();
66116650
Header header = ModelUtils.getReferencedHeader(this.openAPI, headerEntry.getValue());
6612-
Parameter headerParam = new Parameter();
6613-
headerParam.setName(headerName);
6614-
headerParam.setIn("header");
6615-
headerParam.setDescription(header.getDescription());
6616-
headerParam.setRequired(header.getRequired());
6617-
headerParam.setDeprecated(header.getDeprecated());
6618-
headerParam.setStyle(Parameter.StyleEnum.valueOf(header.getStyle().name()));
6619-
headerParam.setExplode(header.getExplode());
6620-
headerParam.setSchema(header.getSchema());
6621-
headerParam.setExamples(header.getExamples());
6622-
headerParam.setExample(header.getExample());
6623-
headerParam.setContent(header.getContent());
6624-
headerParam.setExtensions(header.getExtensions());
6625-
CodegenParameter param = fromParameter(headerParam, imports);
6651+
CodegenParameter param = heeaderToCodegenParameter(header, headerName, imports, mediaTypeSchemaSuffix);
66266652
headers.add(param);
66276653
}
66286654
}
@@ -6638,7 +6664,7 @@ protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Se
66386664
}
66396665
}
66406666
String contentType = contentEntry.getKey();
6641-
CodegenProperty schemaProp = fromProperty(toMediaTypeSchemaName(contentType), mt.getSchema());
6667+
CodegenProperty schemaProp = fromProperty(toMediaTypeSchemaName(contentType, mediaTypeSchemaSuffix), mt.getSchema());
66426668
CodegenMediaType codegenMt = new CodegenMediaType(schemaProp, ceMap);
66436669
cmtContent.put(contentType, codegenMt);
66446670
}
@@ -6666,7 +6692,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
66666692
if (schema == null) {
66676693
throw new RuntimeException("Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): " + body);
66686694
}
6669-
codegenParameter.setContent(getContent(body.getContent(), imports));
6695+
codegenParameter.setContent(getContent(body.getContent(), imports, "RequestBody"));
66706696

66716697
if (StringUtils.isNotBlank(schema.get$ref())) {
66726698
name = ModelUtils.getSimpleRef(schema.get$ref());

0 commit comments

Comments
 (0)