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

[BUG] Parameter with enum is not correctly generated for OAS 3.0 and 3.1 #717

Open
2 of 3 tasks
diei opened this issue Dec 21, 2023 · 3 comments
Open
2 of 3 tasks

Comments

@diei
Copy link

diei commented Dec 21, 2023

Describe the bug

With OAS 2.0 generated JSON of rswag a select box is rendered of enum values for URL parameters. Since I updated to OAS 3 the enum values are not properly processed and no select box is rendered, only an input field. The JSON is generated not as OAS 3 expects it.

Steps to Test or Reproduce

An API with a parameter like described in the following snipped generates a JSON like shown below.

test.rswag_spec.rb:

path '/tests' do
  get 'fetch tests' do
    parameter name: :test_type, in: :query, type: :string, required: false, enum: ['type1', 'type2']

    [...]  
  end
end

Generates:

...
"parameters": [
  {
    "name": "test_type",
    "in": "query",
    "required": false,
    "enum": [
      "type1",
      "type2"
    ],
    "schema": {
      "type": "string"
    }
  }
]
...

Expected behavior

The generated JSON should look like:

...
"parameters": [
  {
    "name": "test_type",
    "in": "query",
    "required": false,
    "schema": {
      "type": "string",
      "enum": [
        "type1",
        "type2"
      ]
    }
  }
]
...

The enum should be in the schema attribute. Then the select box is rendered on the UI page.

Dependency versions

The version of are you using for:

  • Rswag: 2.13.0
  • RSpec-core: 3.12.2
  • Rails: 7.0.8
  • Ruby: 3.2.2

Relates to which version of OAS (OpenAPI Specification)

  • OAS2
  • OAS3
  • OAS3.1
@romanblanco
Copy link
Member

romanblanco commented Dec 21, 2023

@diei, would this work for you?

parameter name: :test_type, in: :query, required: false, schema: { type: :array, items: { enum: ['type1', 'type2'] } }

@diei
Copy link
Author

diei commented Jan 2, 2024

@romanblanco, thank you for the hint. It is better but not like before and not like I need it.

The code...

parameter name: :test_type, in: :query, required: false, schema: { type: :array, items: { enum: ['type1', 'type2'] } }

... generates:

"parameters": [{
  "name": "test_type",
  "in": "query",
  "required": false,
  "schema": {
    "type": "array",
    "items": {
      "enum": [
        "type1",
        "type2"
      ]
    }
  }
}]

That is rendered as a multi-select-box. Then I can select several values at the same time which should not be possible, only one or nothing.

I tried it out a bit and figured out that this works:

parameter name: :test_type, in: :query, required: false, schema: { enum: ['type1', 'type2'] }

That definition generates the expected JSON which is rendered as a common select box:

"parameters": [{
  "name": "test_type",
  "in": "query",
  "required": false,
  "schema": {
      "enum": [
        "type1",
        "type2"
      ]
  }
}]

@diei
Copy link
Author

diei commented Jan 2, 2024

During my many tries to find a solution, I noticed that the type: :string is also not correctly transferred to the JSON:

parameter name: :test_type, in: :query, type: :string, required: false, schema: { enum: ['type1', 'type2'] }

Generates this JSON:

"parameters": [{
  "name": "test_type",
  "in": "query",
  "type": "string",
  "required": false,
  "schema": {
      "enum": [
        "type1",
        "type2"
      ]
  }
}]

But the type should be within schema:

"parameters": [{
  "name": "test_type",
  "in": "query",
  "required": false,
  "schema": {
      "type": "string",
      "enum": [
        "type1",
        "type2"
      ]
  }
}]

If I omit the custom schema definition of the parameter the type is added automatically to the schema or I have to move the type: :string from parameter to the custom schema definition:

parameter name: :test_type, in: :query, required: false, schema: { type: :string, enum: ['type1', 'type2'] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants