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

Unexpected oas3-valid-media-example error on read only fields #2111

Closed
alexg-axis opened this issue Apr 1, 2022 · 1 comment
Closed

Unexpected oas3-valid-media-example error on read only fields #2111

alexg-axis opened this issue Apr 1, 2022 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@alexg-axis
Copy link

Describe the bug

The following error is thrown on a post body's example.

oas3-valid-media-example  "example" property must have required property "id"

The field is indeed required, but it is marked as read only - hence it should not be required in the post body.

To Reproduce

# test.yml
openapi: 3.0.0

info:
  title: Reproduction
  version: 1.0.0

servers:
  - url: http://localhost:8080/api/v1
    description: Development server

paths:
  /test:
    post:
      summary: Create
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Payload'
            example:
              foo: 1
              bar: 2
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Payload'
              example:
                id: 1
                foo: 2
                bar: 3

components:
  schemas:
    Payload:
      type: object
      required:
        - id
        - foo
        - bar
      properties:
        id:
          type: number
          description: Some id
          readOnly: true
        foo:
          type: number
          description: Some field
        bar:
          type: number
          description: Some other field
extends: spectral:oas
# .spectral.yml
spectral lint test.yml
  3:6   warning  info-contact              Info object must have "contact" object.                        info
  3:6   warning  info-description          Info "description" must be present and non-empty string.       info
 13:10  warning  operation-description     Operation "description" must be present and non-empty string.  paths./test.post
 13:10  warning  operation-operationId     Operation must have "operationId".                             paths./test.post
 13:10  warning  operation-tags            Operation must have non-empty "tags" array.                    paths./test.post
 22:21    error  oas3-valid-media-example  "example" property must have required property "id"            paths./test.post.requestBody.content.application/json.example <-- unexpected
 22:21    error  oas3-valid-media-example  "example" property must match exactly one schema in oneOf      paths./test.post.requestBody.content.application/json.example <-- expected given the above error, but otherwise unexpected

Expected behavior

Read only fields that are marked required should not be required in an example when POSTing or PUTing the body.

  3:6   warning  info-contact              Info object must have "contact" object.                        info
  3:6   warning  info-description          Info "description" must be present and non-empty string.       info
 13:10  warning  operation-description     Operation "description" must be present and non-empty string.  paths./test.post
 13:10  warning  operation-operationId     Operation must have "operationId".                             paths./test.post
 13:10  warning  operation-tags            Operation must have non-empty "tags" array.                    paths./test.post

Environment (remove any that are not applicable):

  • Library version: 6.3.0
  • OS: macOS Monterey
@P0lip
Copy link
Contributor

P0lip commented Apr 7, 2022

Hey!
Thank you for reporting the issue!
We already have the same bug report #1274, so I'll close this issue out.
Please leave a thumbs up or subscribe to that issue, I'll keep you posted if we resolve the issue.

@P0lip P0lip closed this as completed Apr 7, 2022
@P0lip P0lip added the duplicate This issue or pull request already exists label Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants