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.43k stars 235 forks source link

oas3-valid-media-example null object is reported as error #2488

Closed LasneF closed 1 year ago

LasneF commented 1 year ago

Describe the bug rules oas3-valid-media-example is too picky with null value

To Reproduce given the below scheam and example

responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                "$ref" : "#/components/schemas/user"
              examples:
                BOB:
                  description: |
                    sample ok
                  value:
                    name: bob
                    address:
                      street : 1 rue de rest
                      city : apicity
                ALICE:
                  description: |
                    sample nok
                  value:
                    name: bob
                    address: null
components:
  schemas:
    user:
      type: object
      properties:
        name: 
          type: string
        address:
          type: object
          properties:
            street :
              type : string
            city: 
              type : string

BOB is correct but ALICE is spot as an ERROR

within the following message error oas3-valid-media-example "address" property type must be object paths./user.get.responses[200].content.application/json.examples.ALICE.value.address

Expected behavior this should not trigger this rules but a dedicated one , may be as warning , that example should not use null value

P0lip commented 1 year ago

Hey! You could change the severity of the rule if you expect some examples to be invalid. In your ruleset, you could change the severity to warn.

"oas3-valid-media-example": "warn"

Alternatively, if address is not a required property in the user schema, you could skip the address in the example in order to avoid the violation of that rule.

LMK if you have any questions.

LasneF commented 1 year ago

according to AJV , if schema is set to object , the payload cannot be null

so here the solution is to set address: type: [ object , null ]

there are severals interpretations here, #no academic battle :) , i close the issue

thx