Skip to content
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

Discriminators do not obey writeOnly #882

Closed
mramato opened this issue Apr 9, 2019 · 3 comments
Closed

Discriminators do not obey writeOnly #882

mramato opened this issue Apr 9, 2019 · 3 comments

Comments

@mramato
Copy link

mramato commented Apr 9, 2019

I wanted to use a discriminator as part of a POST call, but not include the discriminator property as part of the response. I added writeOnly to everywhere it was legal to do so, but the property still shows up. This is also true for the properties on the discriminated object which are also marked readonly.

In the below screenshot, optionsType is a writeOnly property.

image

Here's a reduced test case:

openapi: 3.0.2

info:
  title: Discriminators do not obey `writeOnly`
  version: v1
servers:
  - url: http://localhost

paths:
  /v1/items:
    post:
      summary: Create an item
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Item"
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Item"
components:
  schemas:
    Options1:
      type: object
      allOf:
        - $ref: '#/components/schemas/Item'
        - properties:
            options1Property:
              type: boolean
              writeOnly: true
              default: true
    Options2:
      type: object
      allOf:
        - $ref: '#/components/schemas/Item'
        - properties:
            options2Property:
              type: boolean
              writeOnly: true
              default: false
    Item:
      type: object
      discriminator:
        propertyName: optionType
        mapping:
          Options1: "#/components/schemas/Options1"
          Options2: "#/components/schemas/Options2"
      properties:
        id:
          type: integer
          example: 2194
          readOnly: true
        optionType:
          type: string
          writeOnly: true
          enum:
          - Options1
          - Options2

EDIT: Forgot to mention I'm using the official CDN hosted version of v2.0.0-rc.4

@flaksp
Copy link
Contributor

flaksp commented Apr 10, 2019

Seems like property used as discriminator cannot have modifiers such as readOnly or writeOnly.

As specifications says:

While composition offers model extensibility, it does not imply a hierarchy between the models. To support polymorphism, the OpenAPI Specification adds the discriminator field. When used, the discriminator will be the name of the property that decides which schema definition validates the structure of the model. As such, the discriminator field MUST be a required field.

Your response object contains discriminator definition, but you're trying to hide discriminator field (optionsType) using writeOnly modifier. If you're using discriminator, it should be presented in your schema.

@mramato
Copy link
Author

mramato commented Apr 10, 2019

Thanks for the explanation. What's interesting is that none of the validators/linters seem to complain about this, but that's a completely separate issue.

Sorry for the noise.

@mramato mramato closed this as completed Apr 10, 2019
@RomanHotsiy
Copy link
Member

RomanHotsiy commented Apr 10, 2019

Awesome, no worries.

Thanks, @neluzhin! 🙏

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

3 participants