Redocly / Redocly/openapi-sampler

OneOf not generating all options

Open
#145 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
228
Forks
52
Avg merge
48m
Merged PRs (30d)
6

Description

Hi, thanks for providing such a library for the community. We are using redoc in our code and we found a potential issue in the generated JSON and we traced it down to this library. I compared the OpenAPI doc we generate using the https://editor.swagger.io/ to see if we were diverging from what is expected and apparently it is a bug here in openapi-sampler.

It happens when we use the OneOf and it generates the wrong output. To best exemplify, I created a test for the object.spec.js file.

Please let me know what you think.

  it('should generate both items of oneOf', () => {
    res = sampleObject({
      'type': 'object',
      'properties': {
        'my_obj': {
          'type': 'object',
          'properties': {
            'elements': {
              'items': {
                'oneOf': [
                  {
                    'type': 'object',
                    'properties': {
                      'name': {
                        'type': 'string',
                        'description': '',
                        'enum': [
                          'obj_a'
                        ]
                      }
                    },
                    'title': 'obj_a',
                    'required': [
                      'name'
                    ]
                  },
                  {
                    'type': 'object',
                    'properties': {
                      'name': {
                        'type': 'string',
                        'description': '',
                        'enum': [
                          'obj_b'
                        ]
                      }
                    },
                    'title': 'obj_b',
                    'required': [
                      'name'
                    ]
                  }
                ]
              },
              'type': 'array',
              'minItems': 2,
              'maxItems': 2,
              'uniqueItems': true
            }
          },
          'title': '',
          'required': [
            'elements'
          ]
        }
      },
      'required': [
        'my_obj'
      ]
    })

    expect(res).deep.equal({
      'my_obj': {
        'elements': [
          {
            'name': 'obj_a'
          },
          {
            'name': 'obj_b'
          }
        ]
      }
    })
  });

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the object.spec.js test shown in the issue and inspect the sampleObject call for the array items using oneOf. The change is done when the test produces both objects, with names obj_a and obj_b, in the elements array as shown in the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.