Response Object with optional empty response via content negotiation. #5067
|
How would I model a Response Object where one of the My API uses RFC7240 this would result in say a POST /foo
content-type: application/json
prefer: return=minimal
{"foo": "bar"}
HTTP/1.1 201
preference-applied: return=minimal
location: http://example.com/foo/123or POST /foo
content-type: application/json
prefer: return=representation
{"foo": "bar"}
HTTP/1.1 201
preference-applied: return=minimal
location: http://example.com/foo/123
content-type: application/json
{"foo": "bar"}The closest i can get is the following: ...
post:
parameters:
- name: prefer
in: header
required: false
schema:
type: string
enum: [ "return=minimal", "return=representation" ]
requestBody:
content:
application/json:
schema:
type: object
properties:
foo:
type: str
required: true
responses:
"201":
headers:
location:
schema:
type: string
format: uri
preference-applied:
schema:
type: string
enum: [ "return=minimal", "return=representation" ]
content:
application/json:
schema:
type: object
properties:
foo:
type: str
...but that implies that the and further, is there any way to tie the |
Replies: 2 comments 5 replies
|
This is not an answer to your question, but rather an observation for the purpose of addressing this problem in the future: the problem is that for requests, "requestBody" is split out as a property of its own, allowing As a potential stopgap solution, you could perhaps do this: ...
responses:
"201":
headers:
...
content:
application/json:
description: "used for prefer: return=representation"
schema:
type: object
properties:
foo:
type: str
*/*:
description: "no response body is returned for prefer: return=minimal"
schema: false |
|
RFC7240 4.2 suggests using a 204 No Content response when a minimal preference is an empty representation. It also allows 200 or other appropriate codes but does not define the representation shape. Thus, a solution may be to use both codes POST /foo HTTP/1.1
prefer: return=minimal
{ "foo": "bar" }
HTTP/1.1 204 No Content
preference-applied: return=minimal
location: uri
link: <uri>; rel="self"POST /foo HTTP/1.1
prefer: return=representation
{ "foo": "bar" }
HTTP/1.1 201 Created
preference-applied: return=representation
location: uri
link: <uri>; rel="self"
{ "foo": "bar" }openapi: 3.2.0
info:
title: blah
version: V1
paths:
'/things':
post:
responses:
'201':
description: Created
headers:
preference-applied:
schema:
type: string // this could be more than just minimal or representation so it's open-ended.
content:
'application/json':
schema:
type: object
properties:
foo:
type: string
examples:
representation:
value:
foo: bar
'204':
description: No Content
headers:
preference-applied:
schema:
enum:
- 'return="minimal"'
content:
'application/json':
schema: false
|
This is not an answer to your question, but rather an observation for the purpose of addressing this problem in the future: the problem is that for requests, "requestBody" is split out as a property of its own, allowing
"required":falseto be used when it is relevant, but there is nowhere to specify that a response body is optional because its "content" property is at the same level as the response headers. One possible solution is to add a "responseBody" underneath, which could have a "required" property, adjacent to "content".As a potential stopgap solution, you could perhaps do this: