-
Couldn't load subscription status.
- Fork 716
Description
I'm trying the "SwaggerSample" from the repository from the tagged 3.1.0 release, except I changed it to use media type versioning by specifying the MediaTypeVersionReader as shown here:
services.AddApiVersioning(
options =>
{
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
options.ReportApiVersions = true;
options.ApiVersionReader = new MediaTypeApiVersionReader();
} );I started up the service and went to the Swagger UI, selected the V2 spec and tried to do a GET on /api/Orders through the Swagger UI. This failed with a 400 and a response body of:
{
"error": {
"code": "ApiVersionUnspecified",
"message": "An API version is required, but was not specified.",
"innerError": null
}
}The accept header is set to "application/json" (instead of "application/json; v=2.0") in the request: curl -X GET "http://localhost:5000/api/Orders" -H "accept: application/json", and I can see no way of changing that through the Swagger UI since "application/json" is the only option in the "Response content type" drop down.
I think the options in the "Response context type" are driven by the "produces" field in the swagger.json. See the swagger.json below:
{"swagger":"2.0","info":{"version":"2.0","title":"Sample API","description":"A sample application with Swagger, Swashbuckle, and API versioning.","termsOfService":"Shareware","contact":{"name":"Bill Mei","email":"bill.mei@somewhere.com"},"license":{"name":"MIT","url":"https://opensource.org/licenses/MIT"}},"paths":{"/api/Orders":{"get":{"tags":["Orders"],"summary":"Retrieves all orders.","operationId":"Get","consumes":[],"produces":["application/json"],"parameters":[],"responses":{"200":{"description":"The successfully retrieved orders.","schema":{"uniqueItems":false,"type":"array","items":{"$ref":"#/definitions/Order"}}}}},"post":{"tags":["Orders"],"summary":"Places a new order.","operationId":"Post","consumes":["application/json-patch+json; v=2.0","application/json; v=2.0","text/json; v=2.0","application/*+json; v=2.0"],"produces":["application/json"],"parameters":[{"name":"order","in":"body","description":"The order to place.","required":false,"schema":{"$ref":"#/definitions/Order"}}],"responses":{"201":{"description":"The order was successfully placed.","schema":{"$ref":"#/definitions/Order"}},"400":{"description":"The order is invalid.","schema":{"$ref":"#/definitions/ProblemDetails"}}}}},"/api/Orders/{id}":{"get":{"tags":["Orders"],"summary":"Gets a single order.","operationId":"GetOrderById-V2","consumes":[],"produces":["application/json"],"parameters":[{"name":"id","in":"path","description":"The requested order identifier.","required":true,"type":"integer","format":"int32"}],"responses":{"200":{"description":"The order was successfully retrieved.","schema":{"$ref":"#/definitions/Order"}},"400":{"description":"Bad Request","schema":{"$ref":"#/definitions/ProblemDetails"}},"404":{"description":"The order does not exist.","schema":{"$ref":"#/definitions/ProblemDetails"}}}}},"/api/v{api-version}/People":{"get":{"tags":["People"],"summary":"Gets all people.","operationId":"Get","consumes":[],"produces":["application/json"],"parameters":[{"name":"api-version","in":"path","required":true,"type":"string"}],"responses":{"200":{"description":"The successfully retrieved people.","schema":{"uniqueItems":false,"type":"array","items":{"$ref":"#/definitions/Person"}}}}}},"/api/v{api-version}/People/{id}":{"get":{"tags":["People"],"summary":"Gets a single person.","operationId":"GetPersonByIdV2","consumes":[],"produces":["application/json"],"parameters":[{"name":"id","in":"path","description":"The requested person identifier.","required":true,"type":"integer","format":"int32"},{"name":"api-version","in":"path","required":true,"type":"string"}],"responses":{"200":{"description":"The person was successfully retrieved.","schema":{"$ref":"#/definitions/Person"}},"404":{"description":"The person does not exist.","schema":{"$ref":"#/definitions/ProblemDetails"}}}}}},"definitions":{"Order":{"description":"Represents an order.","required":["customer"],"type":"object","properties":{"id":{"format":"int32","description":"Gets or sets the unique identifier for the order.","type":"integer"},"createdDate":{"format":"date-time","description":"Gets or sets the date and time when the order was created.","type":"string"},"effectiveDate":{"format":"date-time","description":"Gets or sets the date and time when the order becomes effective.","type":"string"},"customer":{"description":"Gets or sets the name of the ordering customer.","type":"string"}}},"ProblemDetails":{"type":"object","properties":{"type":{"type":"string"},"title":{"type":"string"},"status":{"format":"int32","type":"integer"},"detail":{"type":"string"},"instance":{"type":"string"}},"additionalProperties":{"type":"object"}},"Person":{"description":"Represents a person.","required":["firstName","lastName"],"type":"object","properties":{"id":{"format":"int32","description":"Gets or sets the unique identifier for a person.","type":"integer"},"firstName":{"description":"Gets or sets the first name of a person.","maxLength":25,"minLength":0,"type":"string"},"lastName":{"description":"Gets or sets the last name of a person.","maxLength":25,"minLength":0,"type":"string"},"email":{"description":"Gets or sets the email address for a person.","type":"string"}}}}}The "consumes" fields seem to have the v=2.0 media type parameter, but not the "produces" fields.