Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feature/success code responses #7674

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

Open
ignaciomolina wants to merge 7 commits into swagger-api:master
base: master
Choose a base branch
Loading
from ignaciomolina:feature/successCodeResponses
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CodegenOperation {
isResponseBinary = false, isResponseFile = false, hasReference = false,
isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
isRestful, isDeprecated;
public int successCode = 200;
public String path, operationId, returnType, httpMethod, returnBaseType,
returnContainer, summary, unescapedNotes, notes, baseName, defaultResponse, discriminator;
public List<Map<String, String>> consumes, produces, prioritizedContentTypes;
Expand All @@ -43,6 +44,15 @@ public class CodegenOperation {
public String operationIdCamelCase; // for class names
public String operationIdSnakeCase;

/**
* Get http code for success responses
*
* @return success code, 200 by default
*/
public int getSuccessCode() {
return successCode;
}

/**
* Check if there's at least one parameter
*
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1985,20 +1985,20 @@ protected void setNonArrayMapProperty(CodegenProperty property, String type) {
* @param responses Swagger Operation's responses
* @return default method response or <tt>null</tt> if not found
*/
protected Response findMethodResponse(Map<String, Response> responses) {
protected Entry<String, Response> findMethodResponse(Map<String, Response> responses) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand your code correctly, this seems to be a change to the CORE of swagger codegen since it change the way we handle return status. At the beginning, I though it was only Play Framework generator that was missing feature to return the good code but since you needed to add stuff in the DefaultCodegen, I guess it was missing from all codegen.

That is making this change request more important and I think that core members should review it too.

@wing328 Can you check this and also tag some core members to review this.

Thanks!

tzimisce012 and ignaciomolina reacted with thumbs up emoji

String code = null;
for (String responseCode : responses.keySet()) {
if (responseCode.startsWith("2") || responseCode.equals("default")) {
if (code == null || code.compareTo(responseCode) > 0) {
code = responseCode;
Entry<String, Response> response = null;
for (Entry<String, Response> entry : responses.entrySet()) {
if (entry.getKey().startsWith("2") || "default".equals(entry.getKey())) {
if (response == null || response.getKey().compareTo(entry.getKey()) > 0) {
response = entry;
}
}
}
if (code == null) {
if (response == null) {
return null;
}
return responses.get(code);
return response;
}

/**
Expand Down Expand Up @@ -2133,80 +2133,84 @@ public CodegenOperation fromOperation(String path,
}

if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
Response methodResponse = findMethodResponse(operation.getResponses());

for (Map.Entry<String, Response> entry : operation.getResponses().entrySet()) {
Response response = entry.getValue();
CodegenResponse r = fromResponse(entry.getKey(), response);
r.hasMore = true;
if (r.baseType != null &&
!defaultIncludes.contains(r.baseType) &&
!languageSpecificPrimitives.contains(r.baseType)) {
imports.add(r.baseType);
}
r.isDefault = response == methodResponse;
op.responses.add(r);
if (Boolean.TRUE.equals(r.isBinary) && Boolean.TRUE.equals(r.isDefault)){
op.isResponseBinary = Boolean.TRUE;
}
if (Boolean.TRUE.equals(r.isFile) && Boolean.TRUE.equals(r.isDefault)){
op.isResponseFile = Boolean.TRUE;
}
}
op.responses.get(op.responses.size() - 1).hasMore = false;
Entry<String, Response> methodResponse = findMethodResponse(operation.getResponses());

if (methodResponse != null) {
if (methodResponse.getSchema() != null) {
CodegenProperty cm = fromProperty("response", methodResponse.getSchema());

Property responseProperty = methodResponse.getSchema();

if (responseProperty instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) responseProperty;
CodegenProperty innerProperty = fromProperty("response", ap.getItems());
op.returnBaseType = innerProperty.baseType;
} else if (responseProperty instanceof MapProperty) {
MapProperty ap = (MapProperty) responseProperty;
CodegenProperty innerProperty = fromProperty("response", ap.getAdditionalProperties());
op.returnBaseType = innerProperty.baseType;
} else {
if (cm.complexType != null) {
op.returnBaseType = cm.complexType;
String code = methodResponse.getKey();
if (!"default".equals(code)) {
op.successCode = Integer.parseInt(code);
}

for (Entry<String, Response> entry : operation.getResponses().entrySet()) {
Response response = entry.getValue();
CodegenResponse r = fromResponse(entry.getKey(), response);
r.hasMore = true;
if (r.baseType != null &&
!defaultIncludes.contains(r.baseType) &&
!languageSpecificPrimitives.contains(r.baseType)) {
imports.add(r.baseType);
}
r.isDefault = response == methodResponse.getValue();
op.responses.add(r);
if (Boolean.TRUE.equals(r.isBinary) && Boolean.TRUE.equals(r.isDefault)){
op.isResponseBinary = Boolean.TRUE;
}
if (Boolean.TRUE.equals(r.isFile) && Boolean.TRUE.equals(r.isDefault)){
op.isResponseFile = Boolean.TRUE;
}
}
op.responses.get(op.responses.size() - 1).hasMore = false;

Response response = methodResponse.getValue();
if (response != null) {
if (response.getSchema() != null) {
CodegenProperty cm = fromProperty("response", response.getSchema());

Property responseProperty = response.getSchema();

if (responseProperty instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) responseProperty;
CodegenProperty innerProperty = fromProperty("response", ap.getItems());
op.returnBaseType = innerProperty.baseType;
} else if (responseProperty instanceof MapProperty) {
MapProperty ap = (MapProperty) responseProperty;
CodegenProperty innerProperty = fromProperty("response", ap.getAdditionalProperties());
op.returnBaseType = innerProperty.baseType;
} else {
op.returnBaseType = cm.baseType;
}
}
op.examples = new ExampleGenerator(definitions).generate(methodResponse.getExamples(), operation.getProduces(), responseProperty);
op.defaultResponse = toDefaultValue(responseProperty);
op.returnType = cm.datatype;
op.hasReference = definitions != null && definitions.containsKey(op.returnBaseType);

// lookup discriminator
if (definitions != null) {
Model m = definitions.get(op.returnBaseType);
if (m != null) {
CodegenModel cmod = fromModel(op.returnBaseType, m, definitions);
op.discriminator = cmod.discriminator;
op.examples = new ExampleGenerator(definitions).generate(response.getExamples(), operation.getProduces(), responseProperty);
op.defaultResponse = toDefaultValue(responseProperty);
op.returnType = cm.datatype;
op.hasReference = definitions != null && definitions.containsKey(op.returnBaseType);

// lookup discriminator
if (definitions != null) {
Model m = definitions.get(op.returnBaseType);
if (m != null) {
CodegenModel cmod = fromModel(op.returnBaseType, m, definitions);
op.discriminator = cmod.discriminator;
}
}
}

if (cm.isContainer) {
op.returnContainer = cm.containerType;
if ("map".equals(cm.containerType)) {
op.isMapContainer = true;
} else if ("list".equalsIgnoreCase(cm.containerType)) {
op.isListContainer = true;
} else if ("array".equalsIgnoreCase(cm.containerType)) {
op.isListContainer = true;
if (cm.isContainer) {
op.returnContainer = cm.containerType;
if ("map".equals(cm.containerType)) {
op.isMapContainer = true;
} else if ("list".equalsIgnoreCase(cm.containerType)) {
op.isListContainer = true;
} else if ("array".equalsIgnoreCase(cm.containerType)) {
op.isListContainer = true;
}
} else {
op.returnSimpleType = true;
}
if (languageSpecificPrimitives().contains(op.returnBaseType) || op.returnBaseType == null) {
op.returnTypeIsPrimitive = true;
}
} else {
op.returnSimpleType = true;
}
if (languageSpecificPrimitives().contains(op.returnBaseType) || op.returnBaseType == null) {
op.returnTypeIsPrimitive = true;
}
addHeaders(response, op.responseHeaders);
}
addHeaders(methodResponse, op.responseHeaders);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,21 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);

if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
Response methodResponse = findMethodResponse(operation.getResponses());
Map.Entry<String, Response> methodResponse = findMethodResponse(operation.getResponses());

if (methodResponse != null) {
if (methodResponse.getSchema() != null) {
CodegenProperty cm = fromProperty("response", methodResponse.getSchema());
op.vendorExtensions.put("x-codegen-response", cm);
if(cm.datatype == "HttpContent") {
op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
String successCode = methodResponse.getKey();
if (!"default".equals(successCode)) {
op.successCode = Integer.parseInt(successCode);
}
Response response = methodResponse.getValue();
if (response != null) {
if (response.getSchema() != null) {
CodegenProperty cm = fromProperty("response", response.getSchema());
op.vendorExtensions.put("x-codegen-response", cm);
if (cm.datatype == "HttpContent") {
op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public CodegenOperation fromOperation(String path,
path, httpMethod, operation, definitions, swagger);
if (op.getHasExamples()) {
// prepare examples for Apex test classes
Property responseProperty = findMethodResponse(operation.getResponses()).getSchema();
Property responseProperty = findMethodResponse(operation.getResponses()).getValue().getSchema();
String deserializedExample = toExampleValue(responseProperty);
for (Map<String, String> example : op.examples) {
example.put("example", escapeText(example.get("example")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,21 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);

if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
Response methodResponse = findMethodResponse(operation.getResponses());
Map.Entry<String, Response> methodResponse = findMethodResponse(operation.getResponses());

if (methodResponse != null) {
if (methodResponse.getSchema() != null) {
CodegenProperty cm = fromProperty("response", methodResponse.getSchema());
op.vendorExtensions.put("x-codegen-response", cm);
if(cm.datatype == "HttpContent")
{
op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
String successCode = methodResponse.getKey();
if (!"default".equals(successCode)) {
op.successCode = Integer.parseInt(successCode);
}
Response response = methodResponse.getValue();
if (response != null) {
if (response.getSchema() != null) {
CodegenProperty cm = fromProperty("response", response.getSchema());
op.vendorExtensions.put("x-codegen-response", cm);
if (cm.datatype == "HttpContent") {
op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,21 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);

if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
Response methodResponse = findMethodResponse(operation.getResponses());
Map.Entry<String, Response> methodResponse = findMethodResponse(operation.getResponses());
Copy link
Contributor

Choose a reason for hiding this comment

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

Since your are changing stuff in many other generators, did you make sure to generate all theses generators? Does it create any changes to the samples?

Copy link
Contributor Author

@ignaciomolina ignaciomolina Feb 16, 2018
edited
Loading

Choose a reason for hiding this comment

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

this change affects to other generators but only changes code generated for java play framework, because successCode is used nowhere else


if (methodResponse != null) {
if (methodResponse.getSchema() != null) {
CodegenProperty cm = fromProperty("response", methodResponse.getSchema());
op.vendorExtensions.put("x-codegen-response", cm);
if(cm.datatype == "HttpContent") {
op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
String successCode = methodResponse.getKey();
if (!"default".equals(successCode)) {
op.successCode = Integer.parseInt(successCode);
}
Response response = methodResponse.getValue();
if (response != null) {
if (response.getSchema() != null) {
CodegenProperty cm = fromProperty("response", response.getSchema());
op.vendorExtensions.put("x-codegen-response", cm);
if ("HttpContent".equals(cm.datatype)) {
op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
}
}
}
}
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package {{package}};

import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Results;
import play.mvc.Http;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -257,22 +258,22 @@ public class {{classname}}Controller extends Controller {
stage.thenApply(obj -> {
{{/supportAsync}}
{{^isResponseFile}}
{{#supportAsync}} {{/supportAsync}}JsonNode result = mapper.valueToTree(obj);
{{#supportAsync}} {{/supportAsync}}return ok(result);
{{#supportAsync}} {{/supportAsync}}JsonNode result = mapper.valueToTree(obj);
{{#supportAsync}} {{/supportAsync}}return Results.status({{{successCode}}}, result);
{{/isResponseFile}}
{{#isResponseFile}}
{{#supportAsync}} {{/supportAsync}}return ok(obj);
{{#supportAsync}} {{/supportAsync}}return Results.status({{{successCode}}}, obj);
{{/isResponseFile}}
{{/returnType}}
{{^returnType}}
{{#supportAsync}} {{/supportAsync}}return ok();
{{#supportAsync}} {{/supportAsync}}return Results.status({{{successCode}}});
{{/returnType}}
{{#supportAsync}}
});
{{/supportAsync}}
{{/controllerOnly}}
{{#controllerOnly}}
return ok();
return Results.status({{{successCode}}});
{{/controllerOnly}}
}
{{/operation}}
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /