Skip to content

oas3-valid-schema-example reports error when oneOf is used with additionalProperties: false #2854

@JAdshead

Description

@JAdshead

Description

When validating a document which uses oneOf for a object property combined with additionalProperties: false oas3-valid-schema-example reports an error of Property "[X]" is not expected to be here when there is a correct match on the example.

While I believe this to be a bug Id be happy to be proven wrong as its possible I am missing something.

To Reproduce

Schema:

openapi: 3.0.0
info:
  title: oas3-valid-schema-example
  description: 'example of document oas3-valid-schema-example with oneOf'
  version: '1.0'
  contact:
    name: API Owner
servers:
  - url: 'https://api.example.com'
paths:
  /my-api:
    get:
      description: 'Example endpoint with oneOf in response'
      operationId: getExampleWithOneOf
      summary: 'Get example with oneOf'
      responses:
        '200':
          description: 'Successful response'
          content:
            application/json:
              schema:
                type: object
                additionalProperties: false
                oneOf:
                  - type: 'object'
                    additionalProperties: false
                    required:
                      - 'url'
                    properties:
                      url:
                        type: 'string'
                      width:
                        type: 'integer'
                      height:
                         type: 'integer'
                example:
                  url: 'images/38.png'
                  width: 100
                  height: 100

ruleset.yaml file extending oas ruleset:

extends: ["spectral:oas"]
npx @stoplight/spectral-cli lint --ruleset=ruleset.yaml ./valid-schema-example-document.yml

Results in:

./valid-schema-example-document.yml
 43:25    error  oas3-valid-schema-example  Property "url" is not expected to be here          paths./my-api.get.responses[200].content.application/json.schema.example

Expected behavior

I would expect that no error is reported as url is valid.

Additional context

When providing an invalid example e.g. changing url to urls the reported errors are what i would expect

given

example:
  urls: 'images/38.png'
  width: 100
  height: 100

results in:

36:25    error  oas3-valid-schema-example  "example" property must have required property "url"       paths./my-api.get.responses[200].content.application/json.schema.example
 36:25    error  oas3-valid-schema-example  "example" property must match exactly one schema in oneOf  paths./my-api.get.responses[200].content.application/json.schema.example

Can add the following to the rules tests to also reproduce.

{
  name: `${field} containing a valid complex example using oneOf`,
  document: {
    openapi: '3.0.2',
    [field]: {
      schema: {
        type: 'object',
        properties: {},
        additionalProperties: false,
        oneOf: [{
          type: 'object',
          additionalProperties: false,
          required: ['url'],
          properties: {
            url: {
              type: 'string',
            },
            width: {
              type: 'integer',
            },
            height: {
              type: 'integer',
            },
          },
        }],
        example: {
          url: 'images/38.png',
          width: 100,
          height: 100,
        },
      },
    },
  },
  errors: [],
},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions