Skip to content

Commit

Permalink
Feat adds content and header properties to CodegenResponse (#11046)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
spacether authored Dec 10, 2021
1 parent c94d2b2 commit cee5f75
Show file tree
Hide file tree
Showing 143 changed files with 365 additions and 86 deletions.
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());
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

0 comments on commit cee5f75

Please sign in to comment.