-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[Spring] Add an option to return success code #1197
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
[Spring] Add an option to return success code #1197
Conversation
Remove the spacer "{{#async}} ... {{/async}}" "{{^async}} ... {{/async}}"
@bbdouglas (2017/07) @JFCote (2017/08) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) |
ApiUtil.setExampleResponse(request, "application/xml", "<Order> <id>123456789</id> <petId>123456789</petId> <quantity>123</quantity> <shipDate>2000-01-23T04:56:07.000Z</shipDate> <status>aeiou</status> <complete>true</complete></Order>"); | ||
break; | ||
} | ||
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad indent
{{#jdk8}} | ||
{{#async}} | ||
{{#-first}} | ||
{{#jdk8}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is broken
I'm not sure about this one : why would we pick a specific status code over another ? |
Thanks for your review! |
Then I guess you would rather have a |
We could have an application property where you set the default HTTP code you want to get. |
The following is the spec that I'm working on. The each endpoint differently have the HTTP code a stub server should returns. ( openapi: 3.0.1
info:
title: 匿名掲示板API
version: 1.0.0
paths:
/posts:
get:
description: 投稿をすべて取得する
responses:
'200':
description: 投稿の配列
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Post'
post:
...
responses:
'201':
description: 作成した投稿
content:
application/json:
schema:
$ref: '#/components/schemas/Post'
'422':
$ref: '#/components/responses/UnprocessableEntity'
/posts/{postId}:
delete:
description: 投稿を削除する
parameters:
- $ref: '#/components/parameters/postIdParam'
responses:
'204':
description: 投稿の削除に成功
'404':
$ref: '#/components/responses/NotFound'
...
|
Is it really important for your client ? Clients should accept any 2xx codes as successful responses (as per Postel's law) |
As you say from the view of client, it may not important. Client can accept 2xx codes as successful responses. |
Ok. Then we would need some kind of "success code" resolution. I wonder if we don't already have that for some gens. |
I've fixed the broken indentation. Please have a look when you have time. |
ping @cbornet 😉 |
I still think we shouldn't send codes like |
@@ -1,35 +1,38 @@ | |||
{{^reactive}} | |||
int statusCode = {{#responses}}{{#-first}}{{{code}}}{{/-first}}{{/responses}}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be inlined in the ResponseEntity which would make the PR much simpler.
That's a thought. I've noticed two use cases of the generated server codes:
|
Yes, for scaffolding |
Good idea. 💡 👇In my mind, mustache file will be updated like below. Are we on the same page?
- return new ResponseEntity<>(HttpStatus.valueOf(HttpStatus.NOT_IMPLEMENTED));
+ return new ResponseEntity<>(HttpStatus.valueOf(
+ ({{#responses}}{{#-first}}{{{code}}}{{/-first}}{{/responses}} >= 400) ? 200 : {{#responses}}{{#-first}}{{{code}}}{{/-first}}{{/responses}}
+ )); |
I think we could do the resolution in the Java gen (postProcessOperation ?) |
Ah 💡 Thanks! |
Hmm... ideally |
./bin/spring-all-pestore.sh
op.examples = new ExampleGenerator(schemas, openAPI).generateFromResponseSchema(responseSchema, getProducesInfo(openAPI, operation)); | ||
String exampleStatusCode = "200"; | ||
for (String key : operation.getResponses().keySet()) { | ||
if (operation.getResponses().get(key) == methodResponse && !key.equals("default")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
methodResponse
is "2xx" or "default" so exampleStatusCode
will be 2xx.
dc7087a
to
bd7ee46
Compare
Please review the changes when you have time. 🙏 |
As be commented at gitter, I'll make this changes an option. Making the changes an option makes sense to preserve backward compat and covers the two use cases. cc @cbornet |
We have 2 ways how make the changes an option: Generation or Spring config. I'm trying to make a "Generation" option as I'm not familiar with Spring. 🤔💦 |
Changed the PR title. |
@cbornet I've updated this PR, make success code optional. Generated server returns 501 as default. Please review 🙏 |
@@ -25,14 +25,14 @@ return CompletableFuture.supplyAsync(()-> { | |||
{{/-last}} | |||
{{/examples}} | |||
{{^examples}} | |||
return {{#jdk8}}{{#async}}CompletableFuture.completedFuture({{/async}}{{/jdk8}}new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED){{#jdk8}}{{#async}}){{/async}}{{/jdk8}}; | |||
return {{#jdk8}}{{#async}}CompletableFuture.completedFuture({{/async}}{{/jdk8}}new ResponseEntity<>({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}){{#jdk8}}{{#async}}){{/async}}{{/jdk8}}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be HttpStatus.valueOf({{{statusCode}}})
instead of HttpStatus.OK
@@ -43,5 +43,8 @@ exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED); | |||
} | |||
{{/-last}} | |||
{{/examples}} | |||
{{^examples}} | |||
exchange.getResponse().setStatusCode({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be HttpStatus.valueOf({{{statusCode}}})
instead of HttpStatus.OK
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be HttpStatus.OK
as the status code is contained by example. The code above is in {{^examples}}
section.
LGTM 👍 |
Thank you. ✨ |
To be clear the purpose, I've updated the description of this PR. 😌 |
* Returns status code which defined at Response Object * Tweak indent Remove the spacer "{{#async}} ... {{/async}}" "{{^async}} ... {{/async}}" * Update samples^ * Fix broken indentation * Update samples * Revert methodBody.mustache * Revert "Fix broken indentation" * This reverts commit 95b6a00. * Revert "Tweak indent" * This reverts commit ba2cedc. * Revert "Returns status code which defined at Response Object" * This reverts commit f676a89. * Example contains status code * Update samples ./bin/spring-all-pestore.sh * Fix syntax error * Update samples * Run bin/utils/ensure-up-to-date * Make the changes an option `returnSuccessCode` * Run bin/spring-all-pestore.sh to update samples * Run ./bin/utils/export_docs_generators.sh
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.master
,3.4.x
,4.0.x
. Default:master
.Description of the PR
This PR makes the stub server returns status code defined at Response Object instead of 501(Not implemented).This PR adds an option to make the stub server return success code. ( #1197 (comment) )