Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat adds content and header properties to CodegenResponse #11046

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

public class CodegenResponse implements IJsonSchemaValidationProperties {
public final List<CodegenProperty> headers = new ArrayList<CodegenProperty>();
private List<CodegenParameter> responseHeaders = new ArrayList<CodegenParameter>();
public String code;
public boolean is1xx;
public boolean is2xx;
Expand Down Expand Up @@ -87,6 +88,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
private boolean hasDiscriminatorWithNonEmptyMapping;
private CodegenComposedSchemas composedSchemas;
private boolean hasMultipleTypes = false;
private LinkedHashMap<String, CodegenMediaType> content;

@Override
public int hashCode() {
Expand All @@ -98,7 +100,7 @@ public int hashCode() {
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(),
is1xx, is2xx, is3xx, is4xx, is5xx, additionalPropertiesIsAnyType, hasVars, hasRequired,
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes);
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, responseHeaders, content);
}

@Override
Expand Down Expand Up @@ -147,6 +149,8 @@ public boolean equals(Object o) {
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() &&
getHasRequired() == that.getHasRequired() &&
Objects.equals(content, that.getContent()) &&
Objects.equals(responseHeaders, that.getResponseHeaders()) &&
Objects.equals(composedSchemas, that.getComposedSchemas()) &&
Objects.equals(vars, that.vars) &&
Objects.equals(requiredVars, that.requiredVars) &&
Expand Down Expand Up @@ -176,6 +180,22 @@ public boolean equals(Object o) {

}

public LinkedHashMap<String, CodegenMediaType> getContent() {
return content;
}

public void setContent(LinkedHashMap<String, CodegenMediaType> content) {
this.content = content;
}

public List<CodegenParameter> getResponseHeaders() {
return responseHeaders;
}

public void setResponseHeaders(List<CodegenParameter> responseHeaders) {
this.responseHeaders = responseHeaders;
}

@Override
public String getPattern() {
return pattern;
Expand Down Expand Up @@ -488,6 +508,8 @@ public String toString() {
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
sb.append(", composedSchemas=").append(composedSchemas);
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
sb.append(", responseHeaders=").append(responseHeaders);
sb.append(", content=").append(content);
sb.append('}');
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3959,6 +3959,20 @@ public CodegenOperation fromOperation(String path,
ApiResponse response = operationGetResponsesEntry.getValue();
addProducesInfo(response, op);
CodegenResponse r = fromResponse(key, response);
Map<String, Header> headers = response.getHeaders();
if (headers != null) {
List<CodegenParameter> responseHeaders = new ArrayList<>();
for (Entry<String, Header> entry: headers.entrySet()) {
String headerName = entry.getKey();
Header header = entry.getValue();
CodegenParameter responseHeader = heeaderToCodegenParameter(header, headerName, imports, String.format(Locale.ROOT, "%sResponseParameter", r.code));
responseHeaders.add(responseHeader);
}
r.setResponseHeaders(responseHeaders);
}
String mediaTypeSchemaSuffix = String.format(Locale.ROOT, "%sResponseBody", r.code);
r.setContent(getContent(response.getContent(), imports, mediaTypeSchemaSuffix));

if (r.baseType != null &&
!defaultIncludes.contains(r.baseType) &&
!languageSpecificPrimitives.contains(r.baseType)) {
Expand Down Expand Up @@ -4065,6 +4079,7 @@ public CodegenOperation fromOperation(String path,
param = ModelUtils.getReferencedParameter(this.openAPI, param);

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

// ensure unique params
if (ensureUniqueParams) {
Expand Down Expand Up @@ -4502,7 +4517,6 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
codegenParameter.isDeprecated = parameter.getDeprecated();
}
codegenParameter.jsonSchema = Json.pretty(parameter);
codegenParameter.setContent(getContent(parameter.getContent(), imports));

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

protected String toMediaTypeSchemaName(String contentType) {
return toModelName(contentType + "Schema");
protected String toMediaTypeSchemaName(String contentType, String mediaTypeSchemaSuffix) {
return "SchemaFor" + mediaTypeSchemaSuffix + toModelName(contentType);
}

protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Set<String> imports) {
private CodegenParameter heeaderToCodegenParameter(Header header, String headerName, Set<String> imports, String mediaTypeSchemaSuffix) {
if (header == null) {
return null;
}
Parameter headerParam = new Parameter();
headerParam.setName(headerName);
headerParam.setIn("header");
headerParam.setDescription(header.getDescription());
headerParam.setRequired(header.getRequired());
headerParam.setDeprecated(header.getDeprecated());
Header.StyleEnum style = header.getStyle();
if (style != null) {
headerParam.setStyle(Parameter.StyleEnum.valueOf(style.name()));
}
headerParam.setExplode(header.getExplode());
headerParam.setSchema(header.getSchema());
headerParam.setExamples(header.getExamples());
headerParam.setExample(header.getExample());
headerParam.setContent(header.getContent());
headerParam.setExtensions(header.getExtensions());
CodegenParameter param = fromParameter(headerParam, imports);
param.setContent(getContent(headerParam.getContent(), imports, mediaTypeSchemaSuffix));
return param;
}

protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Set<String> imports, String mediaTypeSchemaSuffix) {
if (content == null) {
return null;
}
Expand All @@ -6609,20 +6648,7 @@ protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Se
for (Entry<String, Header> headerEntry: encHeaders.entrySet()) {
String headerName = headerEntry.getKey();
Header header = ModelUtils.getReferencedHeader(this.openAPI, headerEntry.getValue());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've introduced a tiny bug by not calling this at the other place headerToCodegenParameter is called, therefore referenced headers do not generate correctly.
I'm providing a PR to fix this.

Parameter headerParam = new Parameter();
headerParam.setName(headerName);
headerParam.setIn("header");
headerParam.setDescription(header.getDescription());
headerParam.setRequired(header.getRequired());
headerParam.setDeprecated(header.getDeprecated());
headerParam.setStyle(Parameter.StyleEnum.valueOf(header.getStyle().name()));
headerParam.setExplode(header.getExplode());
headerParam.setSchema(header.getSchema());
headerParam.setExamples(header.getExamples());
headerParam.setExample(header.getExample());
headerParam.setContent(header.getContent());
headerParam.setExtensions(header.getExtensions());
CodegenParameter param = fromParameter(headerParam, imports);
CodegenParameter param = heeaderToCodegenParameter(header, headerName, imports, mediaTypeSchemaSuffix);
headers.add(param);
}
}
Expand All @@ -6638,7 +6664,7 @@ protected LinkedHashMap<String, CodegenMediaType> getContent(Content content, Se
}
}
String contentType = contentEntry.getKey();
CodegenProperty schemaProp = fromProperty(toMediaTypeSchemaName(contentType), mt.getSchema());
CodegenProperty schemaProp = fromProperty(toMediaTypeSchemaName(contentType, mediaTypeSchemaSuffix), mt.getSchema());
CodegenMediaType codegenMt = new CodegenMediaType(schemaProp, ceMap);
cmtContent.put(contentType, codegenMt);
}
Expand Down Expand Up @@ -6666,7 +6692,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
if (schema == null) {
throw new RuntimeException("Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): " + body);
}
codegenParameter.setContent(getContent(body.getContent(), imports));
codegenParameter.setContent(getContent(body.getContent(), imports, "RequestBody"));

if (StringUtils.isNotBlank(schema.get$ref())) {
name = ModelUtils.getSimpleRef(schema.get$ref());
Expand Down
Loading