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

False "oas3-valid-media-example" error reported: "property must match exactly one schema in oneOf" #2612

Open
JPPortier opened this issue Apr 11, 2024 · 3 comments
Labels
p/medium t/bug Something isn't working triaged

Comments

@JPPortier
Copy link

Describe the bug

When validating an example with oneOf schema, an oas3-valid-media-exampleerror is reported even if correct

To Reproduce

  1. Given this OpenAPI/AsyncAPI document
    spectral-issue-poc.yaml file content:
openapi: 3.0.2
info:
  description: POC
  title: POC
  version: "1.0"
  license:
    name: MIT
    url: "https://www.poc.com"
  contact:
    email: support@poc.com
    name: support at poc
    url : "https://www.poc.com"
servers:
  - url: https://foo.com
security:
  - Basic: []
tags:
  - description: foo description
    name: Poc
paths:
  '/poc':
    post:
      description: foo description
      tags:
        - Poc
      summary: Trigger a poc
      operationId: poc-operation
      requestBody:
        $ref: "#/components/requestBodies/poc-request"
      responses:
        "200":
          description: "Done"
components:
  examples:

    poc-sample:
      summary: poc sample
      value:
        message:
          text_message:
            text: "This is a text message."

  requestBodies:
    poc-request:
      description: This is the request body for sending a message. `app_id`, `recipient`, and `message` are all required fields.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/poc-payload-request"
          examples:
            sample:
              $ref: "#/components/examples/poc-sample"
      required: true

  schemas:
    poc-payload-request:
      type: object
      required:
        - "message"
      properties:
        message:
          $ref: "#/components/schemas/message-types"

    message-types:
      type: object
      oneOf:
        - $ref: "#/components/schemas/message-type-foo"
        - $ref: "#/components/schemas/message-type-text"

    message-type-foo:
      type: object
      properties:
        foo_message:
          type: object
          required:
            - foo
          properties:
            foo:
              type: string
    message-type-text:
      type: object
      properties:
        text_message:
          type: object
          required:
            - text
          properties:
            text:
              type: string

  securitySchemes:
    Basic:
      type: http
      scheme: basic

And this ruleset.yaml file:

extends: ["spectral:oas"]
  1. Run this CLI command '....'
spectral lint "spectral-issue-poc.yaml" --ruleset ruleset.yaml
  1. See error
spectral-issue-poc.yaml
 39:17  error  oas3-valid-media-example  "message" property must match exactly one schema in oneOf  components.examples.poc-sample.value.message

✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints)

Expected behavior
Example is compliant with spec and error should not been reported

Environment (remove any that are not applicable):

Additional context
Add any other context about the problem here.

@mnaumanali94 mnaumanali94 added t/bug Something isn't working p/medium labels May 17, 2024
@samcrutt9900
Copy link

@mnaumanali94 did you manage to find out the root cause of this issue when triaging it ?

@samcrutt9900
Copy link

samcrutt9900 commented Sep 6, 2024

Interestingly it does validate correctly if you use a single OAS example:

requestBodies:
    poc-request:
      description: This is the request body for sending a message. `app_id`, `recipient`, and `message` are all required fields.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/poc-payload-request"
          example:
             message:
              text_message:
              text: "This is a text message."
      required: true

@piotr-cz
Copy link

piotr-cz commented Sep 27, 2024

@samcrutt9900

Reducing examples to example didn't help in my case, it might not be related

Update: I got this warning because I have use oneOf for integer and float
As described here and here I switched to anyOf

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

No branches or pull requests

4 participants