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

Manually edited oneOf in response schema is overridden #173

Closed
simba909 opened this issue Feb 6, 2024 · 4 comments
Closed

Manually edited oneOf in response schema is overridden #173

simba909 opened this issue Feb 6, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@simba909
Copy link

simba909 commented Feb 6, 2024

I'm evaluating this gem for a project I'm working on and it looks much nicer than rswag. Well done! 👏🏻

Sorry for the long message that follows.

Now, my Rails app has a model that itself has a property that can be an instance of several different sub-models. The JSON for this can look something like:

{
  "name": "some_name",
  "data": {
    "email": "someone@example.com"
  }
}

or:

{
  "name": "some_name",
  "data": {
    "selfie_url": "https://somewhere.com/public/image.jpg"
  }
}

I have request specs that return both variants of this model. By default rspec-openapi generates an OpenAPI spec that looks something like this:

components:
  schemas:
    MyModel:
      type: object
      properties:
        name:
          type: string
        data:
          type: object
          properties:
            email:
              nullable: true
            selfie_url:
              nullable: true

Which is fine but not quite what I want. I'd like to use the oneOf keyword to differentiate the response types. So I edit my OpenAPI spec to look like this:

components:
  schemas:
    MyModel:
      type: object
      properties:
        name:
          type: string
        data:
          oneOf:
            - type: object
              properties:
                email:
                  type: string
                  format: email
            - type: object
               properties:
                 selfie_url:
                   type: string
                   format: uri

But when I rerun the spec generation, rspec-openapi alters the spec to this:

components:
  schemas:
    MyModel:
      type: object
      properties:
        name:
          type: string
        data:
          oneOf:
            - type: object
              properties:
                email:
                  type: string
                  format: email
            - type: object
               properties:
                 selfie_url:
                   type: string
                   format: uri
            type: object
            properties:
              email:
                nullable: true
              selfie_url:
                nullable: true

Notice that it keeps my oneOf: but adds back the sub-model with all properties being nullable. Is this a known limitation or something I'm doing wrong?

@hss-mateus
Copy link
Contributor

Same issue here. Reading through the source code it seems that this case isn't handled.
A possible implementation is to find the most similar schema, merge it, and if no schema is sufficiently similar, a new option is added to oneOf.

@exoego
Copy link
Owner

exoego commented Feb 7, 2024

        data:
          oneOf:
            - type: object
              properties:
                email:
                  type: string
                  format: email
            - type: object
               properties:
                 selfie_url:
                   type: string
                   format: uri
            type: object
            properties:
              email:
                nullable: true
              selfie_url:
                nullable: true

It looks weird. oneOf mixes array elements - ... and object properties type: ....
Was that YAML/JSON generated?

@hss-mateus
Copy link
Contributor

yes, it preserves the array elements and also adds the generated object

@exoego
Copy link
Owner

exoego commented Feb 17, 2024

Closed by #174
Thanks, @hss-mateus

@exoego exoego closed this as completed Feb 17, 2024
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