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

AllowedValues and default should attempt to set the value according to the data type #110

Open
zmay2030 opened this issue Aug 2, 2023 · 1 comment

Comments

@zmay2030
Copy link
Contributor

zmay2030 commented Aug 2, 2023

AllowedValues and DefaultValue(..) should be setting the values according to the data type. Right now, .AllowedValues(...) accepts a map[string]string as the param and the swagger generated is always an enum of strings even if the datatype is boolean or integer.

As for DefaultValue, it accepts a string but it attempts to auto-detect the type despite the data type already being set. So if the DataType is "string" and the default value is set to "1", it will convert it to an integer!

This results in the default value having a different data type than the allowedvalues which is an invalid swagger.

Example 1:

Setting the following:

      .AllowedValues(map[string]string{"0":"0", "1":"1", "2":"2", "3":"3"})
     .DataType("integer")
     .DefaultValue("2")

results in

...
"parameters": [
     {
      "enum": [
       "0",
       "1",
       "3"
      ],
      "type": "integer",
      "default": 1,
      "description": "test",
      "name": "test",
      "in": "query"
     },
...

But what we want is:

...
"parameters": [
     {
      "enum": [
       0,
       1,
       3
      ],
      "type": "integer",
      "default": 1,
      "description": "test",
      "name": "test",
      "in": "query"
     },
...

Example 2:

     .AllowedValues(map[string]string{"true":"true", "false":"false"})
     .DataType("boolean")
     .DefaultValue("true")

Results in

...
"parameters": [
     {
      "enum": [
       "true",
       "false"
      ],
      "type": "integer",
      "default": true,
      "description": "test",
      "name": "test",
      "in": "query"
     },
...

But we want:

...
"parameters": [
     {
      "enum": [
       true,
       false
      ],
      "type": "integer",
      "default": true,
      "description": "test",
      "name": "test",
      "in": "query"
     },
...
zmay2030 pushed a commit to zmay2030/go-restful-openapi that referenced this issue Aug 2, 2023
emicklei pushed a commit that referenced this issue Aug 6, 2023
zmay2030 pushed a commit to zmay2030/go-restful-openapi that referenced this issue Aug 7, 2023
emicklei pushed a commit that referenced this issue Aug 15, 2023
@emicklei
Copy link
Owner

hi, i think relaxing the type of AllowValues argument is possible i.e. from map[string]string to map[string]any. This needs a test though

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