Comments (5)
@matt-gibbs Thanks for the quick reply, Matt. I added those error responses to my Swagger file because I want the users of my API to know specifically what a specific error looks like (an unhandled exception error will be different than a validation error). I read that section of the specification as well and found it vague concerning errors. Quoting from the swagger specification:
However, it is expected from the documentation to cover a successful operation response and any known errors.
I personally assumed that this meant there would only be one successful response and that all other defined responses would be errors. But I think treating any defined 2xx responses as success and 4xx and 5xx as errors would be the best option. There could be an argument whether 1xx and 3xx should be treated as successes or errors.
The default can be used a default response object for all HTTP codes that are not covered individually by the specification.
The Responses Object MUST contain at least one response code, and it SHOULD be the response for a successful operation call.
I think throwing for default is great and definitely something worth preserving. What I would like to suggest is the following:
- Keep the default response the way it is (in my case throwing
HttpOperationException<ErrorResource>
). - If a defined Swagger response is
2xx
, then return the referenced schema (in my case,UserResource
). - If there are multiple
2xx
responses defined with _different_ schemas, then returnobject
. - If a defined Swagger response is
4xx
or5xx
then throwHttpOperationException<>
with the referenced schema (in my caseHttpOperationException<ErrorResource>
for 400 andHttpOperationException<ValidationResource>
for 422). You already generate separate code paths for different "successful" status codes, so something similar could be done for different "error" status codes. A common base type for errors or adding vendor extensions should not be necessary. - If a defined Swagger response is
4xx
or5xx
and a schema _is not_ provided, then throwHttpOperationException
(non-generic).
On another note, awesome work on this this project! I've been able to automatically generate and build an API client project using the OWIN TestServer (to generate the swagger file) and AutoRest.exe (to generate the client code). I then reference that client project in my integration tests to test against my API locally. It's been working great! My suggestions above would help me with testing the appropriate error responses.
from autorest.
I also would like to point out that because those generated operations aren't virtual
, I can't simply create another class that derives from the generated class and overrides those methods.
I've also tried to use PostSharp to intercept the method call to add the logic I want, but since HttpOperationResponse<>
does not derive from a non-generic HttpOperationResponse
class, I'm finding it very difficult to simply pull out the HttpOperationResponse<>
from the Task<HttpOperationResponse<>>
return value so I can get at the HttpResponseMessage
value inside it.
from autorest.
@chartek, Thank you Steven. We're trying to model the behavior described in the Swagger spec.
https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#responses-object
The defined operation responses are "successful" with the default "implying" an error. So the return type for the known codes has just the common base type of object. I agree this is not ideal. I'm going to leave this issue open for us to discuss some options. I think the distinction of throwing for default is good and something we'd like to preserve. Perhaps treating 2xx as success and 4xx as error would be an improvement and allow the common base type to be segmented better. The 201 would have a UserResource return type and the 400 and 422 and default could throw a common base type. Still not ideal, but might be a reasonable improvement without resorting to adding vendor extensions to the Swagger.
from autorest.
@chartek We may consider this in the future. For now this is by design.
from autorest.
Hello guys, sorry to comment on old issue, but I couldn't find any reference for this to a recent issue.
Has this been resolved?
from autorest.
Related Issues (20)
- [v2] Report full error for schema violations
- [v2] Add docs showing how to deploy new versions
- Generate a client from a Swagger URL or page with API
- Autorest does not generate the client with null values
- [openapi-to-tsp][Feature request] Generate response headers defined in swagger
- Handle recent change for tsp arm template
- [openapi to tsp] Fix `accept` header logic
- [openapi-to-typespec] Replace `@projectedName` with `@encodedName` HOT 1
- [openapi-to-typespec] Use `union` instead of `enum`
- [openapi-to-typespec] Create a tool to sort JSON properties to compare swagger diffs
- Non list API requiring a GetAll method in SDK generation
- Pass compilation for all the test projects
- Compilation fail with error " didn't specify which versions with @useDependency."
- @versioned is used without module import
- ApiVersion as path parameter not supported in openapi-to-typespec
- @encodedName don't support "json" as mime type
- Express resource hierarchy
- error | Modelerfour/IncompatibleRequestBodies | Operation 'Imports/PostImportInGroup' content types [json, multipart] have the same body schema but cannot be used together
- AnyObject as body is not correctly generated to typespec
- parent resource should be a model in the generated tsp
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autorest.