Swagger error: additionalProperty: 3XX, 4XX, 5XX - swagger

I have added added few error code in the swagger responses section:
201, 3XX, 400, 401, 4XX, 5XX.
Also, as per swagger2.0 doc you can have:
"The following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX. If a response range is defined using an explicit code, the explicit code definition takes precedence over the range definition for that code."
But still I get the error:
"should NOT have additional properties. additionalProperty: 3XX, 4XX, 5XX"
Any clue?

That quote is from the OpenAPI 3.0 Specification, not 2.0.
The 2.0 spec does not support wildcard response codes. You need to use specific codes, such as 200, 400, 404, etc., and you can use the default response to match all HTTP codes that are not covered individually by your spec.

Related

Swashbuckle refuses to show response example for HTTP 500

I'm using Swashbuckle for a web api app in .Net Core 3.1. I want response examples for various response codes. I can get all of them working except HTTP 500. These are the attributes on the a particular method:
[SwaggerRequestExample(typeof(GroupInfoRequest), typeof(GroupInfoRequestExample))]
[SwaggerResponseExample(Status200OK, typeof(GroupInfo200Example))]
[SwaggerResponseExample(Status400BadRequest, typeof(GroupInfo400Example))]
[SwaggerResponseExample(Status403Forbidden, typeof(GroupInfo403Example))]
[SwaggerResponseExample(Status404NotFound, typeof(GroupInfo404Example))]
[SwaggerResponseExample(Status500InternalServerError, typeof(GroupInfo500Example))]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status400BadRequest)]
[ProducesResponseType(Status403Forbidden)]
[ProducesResponseType(Status404NotFound)]
[ProducesResponseType(Status500InternalServerError)]
I can get all of them to render except the GroupInfo500Example. The application only returns an HTTP 500 to indicate an internal exception that isn't caught by other exception handlers. It is intended to return a body that contains, among other things, a GUID that can be passed in to our support organization to help them look up the exception in the application logs. I can not get the example to render for any 5xx error. If I change it to another status code, it renders, so it's specifically the 5xx result that doesn't render. I've checked the openapi json produced and it's not produced as part of the generated JSON. Is there a filter in place that keeps 5xx response docs from showing response examples?
Finally figured it out. I was missing part of the 'ProducesResponseType' attribute. It needs to have the return type as well as the HTTP status code. This works:
[ProducesResponseType(typeof(ADServiceOperationMultipleResult<GroupActionRequestForUsers, UserQuery>), Status200OK)]
[ProducesResponseType(typeof(ADServiceOperationMultipleResult<GroupActionRequestForUsers, UserQuery>), Status400BadRequest)]
[ProducesResponseType(typeof(ADServiceOperationMultipleResult<GroupActionRequestForUsers, UserQuery>), Status403Forbidden)]
[ProducesResponseType(typeof(ADServiceOperationMultipleResult<GroupActionRequestForUsers, UserQuery>), Status404NotFound)]
[ProducesResponseType(typeof(ADServiceOperationMultipleResult<GroupActionRequestForUsers, UserQuery>), Status422UnprocessableEntity)]
[ProducesResponseType(typeof(ADServiceOperationMultipleResult<GroupActionRequestForUsers, UserQuery>), Status500InternalServerError)]
Oddly enough, some status codes were including the example without it, but now the examples appear consistently as long as I include the method return type in the attribute.

Calling a service but description of the return code does not match what I define explicitly

I am using OpenAPI (Swagger) to define a web service.
In the response section, I define the following code explicitly
responses:
'200':
description: Your order has been placed
content: ...
However, when I call the service, it returns "200 OK", which is a standard http status code description instead of the description I define explicitly
According to here https://swagger.io/docs/specification/describing-responses/, it says "If a response range is defined using an explicit code, the explicit code definition takes precedence over the range definition for that code."
Do I miss/misunderstand something?
The description field in the OpenAPI definition is metadata used for documentation purposes only.
This description is not related to the status text (aka reason-phrase) value in HTTP responses, such as "OK" in "HTTP/1.1 200 OK". Most servers/frameworks use common status text e.g. "200 OK" or "403 Forbidden".

Error testing API with Swagger response header 405

So I'm fairly new to creating API documentation and I'm having some difficulty creating a new entry via the swagger UI. I keep getting a 405 response. I have no idea what the issue is, I've become code blind. The link to the API is below. Any pointers would be greatly appreciated.
https://swaggerhub.com/apis/Sharper-Web-Dev/test/1.0.0
Your definition specifies SwaggerHub's mock server (virtserver.swaggerhub.com) as the target server. The mock server generates the responses based on the responses defined in your API definition. POST /charity defines just the 405 response, that's why the mock returns 405.
To get a "normal" response, you need to define a 200 OK or 201 Created response, and add the response schema (or response examples) describing the desired JSON structure.
paths:
/charity:
post:
...
produces:
- application/json
parameters:
...
responses:
200:
description: OK
schema:
$ref: "#/definitions/MyResponseSchema"
405:
description: "Invalid input"
See How Response Mocking Works in SwaggerHub docs for further information.

What the name of the error domain for HTTP error codes?

I have some network code which behaves differently if an error is due to timeout, bad url, url not found etc, etc. which are all covered by NSURLErrorDomain.
However if I get a http error, such as http 406, then that's not in NSURLErrorDomain. The domain is "HTTP Error", but I don't want to use that as a hardcoded string, I want to use whatever its NSWhateverErrorDomain definition is, but after loads of searching I just can't find what that is anywhere.
Undocumented NSURLErrorDomain error codes (-1001, -1003 and -1004) using StoreKit
Reference:
https://developer.apple.com/reference/cfnetwork/cfnetworkerrors

How to globally handle HTTP errors in Grails (status codes 4xx/5xx)?

Is there a way in Grails to catch all the possible HTTP errors before they're being sent to the client browser, as to be able to handle the content being sent to the client? I mean all 4xx and 5xx HTTP status codes, not only the 403, 404 and 500 like other have suggested.
What I'd like to do is to catch all HTTP errors in the first place, without having to specify them all one by one, then in a second step I would filter (e.g. in an error controller) specific error codes that I would like to handle (e.g. 400), but at least the ones that I would not specify would render to a generic error template I defined.
If this can't be done (or shouldn't be done), which HTTP errors codes should really be checked for and handled? I can at least see those codes happening at some point: 400, 401, 403, 404, 405, 500, 501, 503. And also, how should they be handled, using HTTP response codes mappings?
Thanks!
haven't actually tried it but maybe a number constraint might work?
"$errorCode" {
controller = "errors"
action = "displayError"
constraints {
errorCode(matches:/\d{3}/)
}
}

Resources