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

Bug related to additionalProperties and oneOf #1347

Open
fabioperrella opened this issue Sep 18, 2020 · 3 comments
Open

Bug related to additionalProperties and oneOf #1347

fabioperrella opened this issue Sep 18, 2020 · 3 comments
Labels
t/bug Something isn't working triaged

Comments

@fabioperrella
Copy link

Describe the bug
A clear and concise description of what the bug is.

Spectral is showing the error oas3-valid-oas-content-example in a scenario that I didn't expect

To Reproduce

  1. Given this yaml:
openapi: 3.0.0
tags:
  - name: Preview
info:
  version: 1.0.0
  title: lala
  contact:
    name: Team X
    url: 'http://lala.com'
  description: Lala
servers:
  - url: https://lala.com
    description: lala
components:
  schemas:
    Default:
      type: object
      properties:
        url:
          type: string
          nullable: true
    Audio:
      allOf:
        - $ref: '#/components/schemas/Default'
        - type: object
          properties:
            audio_url:
              type: string
              nullable: false

paths:
  '/lala/{id}':
    parameters:
      - schema:
          type: string
        name: id
        in: path
        required: true
        description: lala
    get:
      description: Returns lala
      tags:
        - Preview
      parameters:
        - in: query
          name: id
          description: 'lala'
          schema:
            type: string
          example: 2c0ed7250ea9c8b65c18dfbf4efdff73b9aedf38
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                properties:
                  previews:
                    type: object
                    additionalProperties:
                      oneOf:
                        - $ref: '#/components/schemas/Default'
                        - $ref: '#/components/schemas/Audio'    
              example:
                previews:
                  abc123aa:
                    url: https://example.com/preview1.jpeg
                  sdja6tsj:
                    url: null
                    audio_url: https://example.com/audio.jpeg
      operationId: lala
      summary: lala
  1. Run this CLI command
docker run --rm -v $PWD:/tmp -it stoplight/spectral lint -v -F hint "/tmp/openapi.yaml"
  1. See error
67:28  error  oas3-valid-oas-content-example  `abc123aa` property should match exactly one schema in oneOf

Expected behavior

I'm pretty sure I specified the example correctly and it shouldn't return an error

Environment (remove any that are not applicable):

  • Library version: stoplight/spectral:latest (0bca0b3aa66a)
  • OS: MacOS
@mnaumanali94
Copy link
Contributor

@P0lip Does spectral support objects in additionalProperties?

@anshul-raman
Copy link

anshul-raman commented May 9, 2024

Hi, I was able to solve this issue by adding a required field in the schema.

@teameh
Copy link

teameh commented Sep 3, 2024

Adding all properties as required is indeed a good workaround.

    Shape:
      oneOf:
        - $ref: '#/components/schemas/Rectangle'
        - $ref: '#/components/schemas/Circle'
      discriminator:
        propertyName: shapeType
        mapping:
          rectangle: '#/components/schemas/Rectangle'
          circle: '#/components/schemas/Circle'
      example:
        shapeType: rectangle
        width: 10
        height: 20

    Rectangle:
      type: object
      required: [shapeType, width, height] <-- all required
      properties:
        shapeType:
          type: string
        width:
          type: number
        height:
          type: number

    Circle:
      type: object
      required: [shapeType, radius] <-- all required
      properties:
        shapeType:
          type: string
        radius:
          type: number

However, this should also validate fine:


    Rectangle:
      type: object
      required: [shapeType] <-- only discriminator is required
      properties:
        shapeType:
          type: string
        width:
          type: number
        height:
          type: number

Instead, this yields "0" property must match exactly one schema in oneOf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t/bug Something isn't working triaged
Projects
None yet
Development

No branches or pull requests

4 participants