Skip to content

Not able to implement Prefer header behavior. #2109

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

Closed
rmedaer opened this issue Jan 17, 2020 · 7 comments
Closed

Not able to implement Prefer header behavior. #2109

rmedaer opened this issue Jan 17, 2020 · 7 comments

Comments

@rmedaer
Copy link

rmedaer commented Jan 17, 2020

The RFC7240 (Section 4.2) defines how-to indicate to the server the preferred representation to return.

  • Either a minimal one: no content, a Location header with URI of full resource representation.
  • Or a representation: returns the resource in response content, (optional Location header?)

I implemented this behavior in my API. But I would like to specify it in my OpenAPI specification.
This is the current specification I wrote:

paths:
  /resources:
    post:
      operationId:  createResource
      parameters:
        - name: Prefer
          in: header
          description: Preferred representation
          required: false
          style: simple
          explode: true
          schema:
            type: object
            required:
              - return
            properties:
              return:
                type: string
                enum:
                  - representation
                  - minimal
                default: representation
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/newResource"
      responses:
        201:
          description: The resource was created successfully.
          headers:
            Location:
              description: The URI of created resource.
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/resource"

Although this is not 100% correct. Indeed, because I'm not able to specify multiple responses with the same code, I can't define the two behaviors depending on Prefer request header.

Btw I don't need to express the "switching" Prefer behavior, but at least I want to express the two possible behaviors/responses.

Maybe related to: #1572

Any thoughts ?

@erunion
Copy link

erunion commented Jan 17, 2020

Your parameters array looks off. It should be:

parameters:
  - name: Prefer
    in: header
    description: Preferred representation
    required: false
    style: simple
    explode: true
    schema:
      type: object
      required:
        - return
      properties:
        return:
          type: string
          enum:
            - representation
            - minimal
          default:
            representation

@handrews
Copy link
Member

@rmedaer Since you don't need to correlate the Prefer values, you could just make the response schema

oneOf:
  - type: string
    maxLength: 0
  - type: object
    required: [..]
    properties: {...}

@rmedaer
Copy link
Author

rmedaer commented Jan 17, 2020

@erunion indeed ! Wrong copy/paste from $ref. I edited the comment ;-)

@handrews well ! Sounds good to me, thank's.
And if I want to "correlate the Prefer values", do you have an idea how to do it ?

@handrews
Copy link
Member

@rmedaer The Preference-Applied response header is how you do it in the message, but since headers and bodies are handled separately and you only get one response object per status code, I don't see any way to correlate it. But I might be missing something.

@handrews
Copy link
Member

You could do an enum for the header and, in this case, it's pretty obvious which header value corresponds, but that's definitely not automatically enforced by anything.

@darrelmiller
Copy link
Member

@rmedaer Create one schema that validates both minimal and representation as Henry suggested and then create two examples and use the key of the example to identify the minimal vs the full representation example.

@rmedaer
Copy link
Author

rmedaer commented Jan 30, 2020

Looks good to me.

Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants