-
Notifications
You must be signed in to change notification settings - Fork 90
Treat all 2xx
status codes as success
#853
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
Comments
We have handled different 2XX if you have defined them in swagger.
We will generate code as below.
|
This assumes that all status codes will be present in the OpenAPI file. Here is what the spec says about this:
Ref: https://spec.openapis.org/oas/latest.html#responses-object |
The OpenAPI specification is in aligned with the HTTP specification which says:
https://datatracker.ietf.org/doc/html/rfc7231#section-6 To paraphrase, clients that make HTTP calls MUST never fail because they get back an unexpected status code. |
@peombwa @darrelmiller Share some of my points.
|
@dolauli The HTTP specification is very clear on what a client MUST do. The OpenAPI specification is in agreement. 201 and 200 can often return the same schema. The content-length header will tell the client if there is a payload for the 201. If there is the shape will likely be the same as 200. 203 is not something an API provider may know about and therefore would not likely ever be listed in the OpenAPI description. 204 has a well defined empty response payload which a client can return a null value. Same for 205. In this case of a 206 the same response payload for 200 will work just fine. 207 responses are likely going to be the same shape as a 200, the only difference being the inclusion of some error messages in the payload. I've never seen anyone use 208 or 226. Failing because of an unexpected status code is wrong according to HTTP and OpenAPI. |
@darrelmiller
|
Yes. Our APIs are described using CSDL. There is no way in CSDL to explicitly list which 2XX status codes will be returned. Exhaustively listing all the status codes that are returned today is bad practice as it encourages clients to couple to these values which prevents a service from returning additional status codes in the future. The reason AutoREST PowerShell is running into this problem is because it made the broken assumption that status codes are listed exhaustively. This issue was discussed and finalized six years ago. OAI/OpenAPI-Specification#568
The meaning of 2XX status codes are well defined in the HTTP specification. Generally the behavior isn't specific to the particular resource.
If there is a JSON response payload and it is a 2XX then the odds are pretty high that the payload is the shape described in the 200 status code. This is why in OpenAPI 3.0 we explicitly introduced the '2XX' status code option.
I don't really understand what the -passthru parameter does, but if the user of AutoREST wants the passthru parameter to appear then they can choose to explicitly list 204 in the OpenAPI description. |
@darrelmiller I can not infer that all 2XX response follow the same response schema with 200 unless it is explicitly defined in swagger. And following is a sample response I copied form msgraph users.yml (https://github.com/microsoftgraph/msgraph-sdk-powershell/blob/dev/openApiDocs/v1.0/Users.yml#L369). And according to openapi specification, if an unlisted response like 206 is returned, it will fall into default which is for the undeclared response, whose schema refers to an error. That's is why we return an error for 206 response. responses:
'200':
description: Retrieved entities
content:
application/json:
schema:
title: Collection of user
type: object
properties:
value:
type: array
items:
$ref: '#/components/schemas/microsoft.graph.user'
'@odata.nextLink':
type: string
additionalProperties:
type: object
default:
$ref: '#/components/responses/error' I partially agree with you that we do not need to list all response status in swagger e.g. 429, 202. But for response with a meaningful response payload to end users, I do think we should explicitly declared them with the schema. |
@dolauli Agreed. If we have We will find another way to resolve this in the meanwhile. We would really appreciate if you could add supporting 2XX to your backlog. |
The generated commands should treat all 2xx HTTP status codes as success, or we should provide an option to specify this behavior via a configuration. With Microsoft Graph API, treating all 2xx status codes as success is generally recommended since some APIs can return any status code in the 2xx range to mean success. e.g., Security APIs can return
200
or206
depending on response from the provider.The OpenAPI files may not always contain all status codes in the response object.
Potential solution
Instead of handling each status code in a switch, we should check if the response message
IsSuccessStatusCode
and call anonSuccess
callback method.Instead of this (before)
Lets to do this (Suggested change)
The text was updated successfully, but these errors were encountered: