stoplightio / spectral

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI v3.1, v3.0, and v2.0 as well as AsyncAPI v2.x.
https://stoplight.io/spectral
Apache License 2.0
2.45k stars 235 forks source link

Bug related to additionalProperties and oneOf #1347

Open fabioperrella opened 3 years ago

fabioperrella commented 3 years ago

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:

  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):

mnaumanali94 commented 2 years ago

@P0lip Does spectral support objects in additionalProperties?

anshul-raman commented 4 months ago

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

teameh commented 3 days ago

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