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

V2 doesn't parse @Param correctly when using user defined struct when #1707

Open
dylanhitt opened this issue Nov 21, 2023 · 1 comment
Open

Comments

@dylanhitt
Copy link

Describe the bug
When attempting to set the value of a @param query to that of a userdefined struct. The number of params parsed is correct (2 fields, 2 params). However they are all the same key. If I gen the spec again it will show up with a different key but still all the same.

To Reproduce
Steps to reproduce the behavior:

  1. Create main.go with this content:
package main

type Student struct {
	Name     string
	Age      int
	Teachers []string
}

// @Param request query Student true "query params"
// @Success 200
// @Router /test [get]
func Fun() {

}
  1. Install latest swag and run
go install github.com/swaggo/swag/v2/cmd/swag@latest
swag init --parseDependency --v3.1
  1. Shows this output
components: {}
externalDocs:
  description: ""
  url: ""
info:
  title: ""
  version: ""
openapi: 3.1.0
paths:
  /test:
    get:
      parameters:
      - in: query
        name: teachers
        schema:
          items:
            type: string
          type: array
      - in: query
        name: teachers
        schema:
          items:
            type: string
          type: array
      - in: query
        name: teachers
        schema:
          items:
            type: string
          type: array
      responses:
        "200":
          description: OK

If you run it repeatedly the name for the query will change. But they will all be the same name.

I attempted to fix this myself, but after adding a unit test in parserv3_test.go, it appears to spit out the correct output. I just did something simple like this: (my the fail I'm targeting is the same as my main.go above)

func TestParseStructParamCommentByQueryTypV2(t *testing.T) {
	t.Parallel()

	searchDir := "testdata/v3/kek"

	expected := `{}`
	p := New(GenerateOpenAPI3Doc(true))
	p.PropNamingStrategy = PascalCase
	err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
	assert.NoError(t, err)

	_, err = p.packages.ParseTypes()
	assert.NoError(t, err)

	result, err := json.Marshal(p.openAPI)
	require.NoError(t, err)

	assert.JSONEq(t, string(expected), string(result))
}

Yes, I'm aware my expect and actual will never equal here. I just wanted to get something to see the diff and it appears fine.

Expected behavior
Params to be each individual key rather then the same one repeated

Your swag version
2.0.0

Your go version
1.21.0 darwin/arm64

Desktop (please complete the following information):

  • OS: MacOs
@5rahim
Copy link

5rahim commented Dec 8, 2024

This is fixed in v2.0.0-rc4

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