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

feat(openapi/explode): explicitly set false in specs #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tbourrelovh
Copy link

@tbourrelovh tbourrelovh commented Jun 18, 2024

Why

Using omitempty with bool type ends up having the configuration not being set in openapi spec file.

As default value for the explode configuration is true (see this page), when we want to set it to false, it is not defined in the spec file.

Example reproducer based on this repository master branch:

package main

import (
        "io/ioutil"
        "log"
        "net/http/httptest"
        "time"

        "github.com/gin-gonic/gin"
        "github.com/loopfz/gadgeto/tonic"
        "github.com/wI2L/fizz"
        "github.com/wI2L/fizz/openapi"
)

type inputModel struct {
        MyQueryParam []string `query:"myQueryParam" explode:"false"`
}

func main() {
        f := fizz.New()

        f.GET("/test", []fizz.OperationOption{
                fizz.InputModel(&inputModel{}),
        }, tonic.Handler(func(c *gin.Context) error {
                return nil
        }, 200))

        infos := &openapi.Info{
                Title:       "Test Server",
                Description: `This is a test server.`,
                Version:     "1.0.0",
        }
        f.GET("/openapi.json", nil, f.OpenAPI(infos, "")) // default is JSON
        srv := httptest.NewServer(f)
        defer srv.Close()

        c := srv.Client()
        c.Timeout = 1 * time.Second

        respJSON, _ := c.Get(srv.URL + "/openapi.json")
        defer respJSON.Body.Close()

        specJSON, _ := ioutil.ReadAll(respJSON.Body)
        log.Println(string(specJSON))
}

json spec output:

{
  "openapi": "3.0.1",
  "info": {
    "title": "Test Server",
    "description": "This is a test server.",
    "version": "1.0.0"
  },
  "paths": {
    "/test": {
      "get": {
        "operationId": "func1",
        "parameters": [
          {
            "name": "myQueryParam",
            "in": "query",
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "style": "form"
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "components": {}
}

As you can see, explode is missing in the myQueryParam parameter configuration, making it impossible to override it's default value which is true.

When I set explode:"true" in inputModel struct, spec file looks like this:

{
  "openapi": "3.0.1",
  "info": {
    "title": "Test Server",
    "description": "This is a test server.",
    "version": "1.0.0"
  },
  "paths": {
    "/test": {
      "get": {
        "operationId": "func1",
        "parameters": [
          {
            "name": "myQueryParam",
            "in": "query",
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "style": "form",
            "explode": true
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  },
  "components": {}
}

With true, swagger will generate the following query parameter string: ?myQueryParam=a&myQueryParam=b, while when set it false, it generates ?myQueryParam=a,b. Where a and b are fake values for myQueryParam.

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

Successfully merging this pull request may close these issues.

1 participant