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

oneOf where more than 2 types inside an array #257

Open
harveyappleton opened this issue Nov 3, 2021 · 7 comments
Open

oneOf where more than 2 types inside an array #257

harveyappleton opened this issue Nov 3, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@harveyappleton
Copy link

Are you using jest or chai?
Jest

Are you using OpenAPI 2, 3.0.X, or 3.1.0?
OpenAPI 3.0.3

Describe the bug clearly
When I use oneOf for items in an array with more than 2 distinct references, it fails randomly and can't discriminate which is the matched entity type (even with discriminator property).

Steps to reproduce the bug:

  1. Create an open api spec with oneOf with more than 2 - for example
List:
  type: array
  items: 
    oneOf:
      - $ref: '#/components/schemas/Account'
      - $ref: '#/components/schemas/Item'
      - $ref: '#/components/schemas/Payment'
  1. From an endpoint, return this array with a mix of these 3 entities, eg
[
   { type: 'Account' },
   { type: 'Item' },
   { type: 'Payment' }
]

(my actual entities are more complex than this, but for the sake of a simple bug description, this will suffice)

  1. It fails on 1 of the 3 entity types, if I remove one and only do 2 entity types then it is fine.

What did you expect to happen instead?
It validates the array correctly

Are you going to resolve the issue?
If I'm pointed in the right direction I may be able to!

@harveyappleton harveyappleton added the bug Something isn't working label Nov 3, 2021
@rwalle61
Copy link
Collaborator

rwalle61 commented Nov 6, 2021

Thanks for raising this @harveyappleton 🙂

I may have time next weekend to look at this. In the meantime if you'd like to recreate this using this file: https://github.com/openapi-library/OpenAPIValidators/blob/master/packages/jest-openapi/__test__/bugRecreationTemplate.test.ts (just clone the repo and post a link to your branch here). That will help speed up my investigation

@harveyappleton
Copy link
Author

Hey @rwalle61 - think I might have found cause of issue AND produced a demo

https://github.com/harveyappleton/OpenAPIValidators/tree/master

Clone this fork
Go into project
cd packages/jest-openapi
yarn test

You should see the failing test

● Recreate bug (issue #257) › passes

    expect(received).toSatisfyApiSpec() // Matches 'received' to a response defined in your API spec, then validates 'received' against it

    expected received to satisfy the '200' response defined for endpoint 'GET /recreate/bug' in your API spec

    received did not satisfy it because: included/1/type must be equal to one of the allowed values, included/1/attributes must have required property 'rating', included/1/attributes must NOT have additional properties, included/1/type must be equal to one of the allowed values, included/1/type must be equal to one of the allowed values, included/1/attributes must have required property 'description', included/1/attributes must NOT have additional properties, included/1 must match exactly one schema in oneOf, included/3/type must be equal to one of the allowed values, included/3/attributes must have required property 'rating', included/3/attributes must NOT have additional properties, included/3/type must be equal to one of the allowed values, included/3/type must be equal to one of the allowed values, included/3/attributes must have required property 'description', included/3/attributes must NOT have additional properties, included/3 must match exactly one schema in oneOf

    received contained: {
      body: {
        included: [
          { type: 'Apple', attributes: { rating: 5 } },
          { type: 'AppleOrange', attributes: { isTasty: true } },
          {
            type: 'Banana',
            attributes: { description: 'Yellow and sumptuous' }
          },
          { type: 'AppleOrange', attributes: { isTasty: false } },
          {
            type: 'Banana',
            attributes: { description: 'Another thing about a banana' }
          }
        ]
      }
    }

It is because it seems to match AppleOrange as Apple, perhaps it is using some sort of string starts with method? Rather than matching the entire string??

If you change AppleOrange to Orange (in both the test file and schema), it will pass!

Schema & test file are in: OpenAPIValidators/blob/master/packages/jest-openapi/__test__ on my branch.

Hope that helps

@rwalle61
Copy link
Collaborator

Thanks @harveyappleton ! I didn't have time to investigate this weekend but you've given me a great starting point for when I can. Seems like something odd is happening

@harveyappleton
Copy link
Author

No problems @rwalle61 - if you can point me in the right direction, I might be able to do a fix for this :)

@asafMasa
Copy link

asafMasa commented Jan 25, 2022

@rwalle61 any progress on the oneOf issues?

@asafMasa
Copy link

I have similar problem with the following:

received did not satisfy it because: response must match exactly one schema in oneOf

received contained: { body: { id: 'e1b051bf-e12e-4c1f-a257-f9de2de8bbfb' } } 

OpenAPI:
The '201' response defined for endpoint 'POST /jobs/{myId}/foos' in API spec: {
  '201': {
    description: 'Foo created successfully',
    content: {
      'application/json': {
        schema: {
          oneOf: [
            { '$ref': '#/components/schemas/createFooResponse' },
            {
              '$ref': '#/components/schemas/createMultipleFoosResponse'
            }
          ]
        }
      }
    }
  }
}

createFooResponse:
  type: object
  properties:
    id:
      $ref: '#/components/schemas/fooId'
createMultipleFoosResponse:
  type: object
  properties:
    ids:
      type: array
      items:
        $ref: '#/components/schemas/fooId'
        
fooId:
  type: string
  format: uuid

@rwalle61
Copy link
Collaborator

rwalle61 commented Mar 7, 2022

Sorry I don't have time to investigate this further, happy for someone else to take a closer look 🙂 we parse the OpenAPI spec and pass the schemas to a 3rd party validator (openapi-response-validator) so it would be good to confirm if the bug is in our code or theirs (in my experience, it is probably ours 😁)

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

No branches or pull requests

3 participants