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: --v3.1 flag: overridden by the specific type of model composition in response are not working correctly #1950

Open
mushding opened this issue Dec 19, 2024 · 0 comments

Comments

@mushding
Copy link

mushding commented Dec 19, 2024

Describe the bug

As READMD.md mentioned below:

// JSONResult's data field will be overridden by the specific type proto.Order
@success 200 {object} jsonresult.JSONResult{data=proto.Order} "desc"

swaggo supports overriding an existing struct with another struct type in comments.

However, in the v2 branch, this feature is not working correctly.

To Reproduce
Steps to reproduce the behavior:

create a simple example:

main.go

import (
	"net/http"

	"swaggo-v2/api"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html


func main() {
	http.HandleFunc("/testapi/data1/", api.GetData1)
	http.HandleFunc("/testapi/data2/", api.GetData2)
	http.ListenAndServe(":8080", nil)
}

api/api.go

package api

import "net/http"

type Response struct {
	httpStatus int         `json:"-"`
	Code       string      `json:"code" example:"0"`
	Msg        string      `json:"message"`
	Data       interface{} `json:"data"`
}

type GetData1Response struct {
	Field1 string `json:"field1" example:"Field1"`
	Field2 string `json:"field2" example:"Field2"`
}

// GetData1 godoc
//
//	@Summary		Get data1
//	@Produce		json
//	@Success		200				{object}	Response{data=GetData1Response}
//	@Router			/internal/data1 [get]
func GetData1(w http.ResponseWriter, r *http.Request) {
	//write your code
}

type GetData2Response struct {
	Field3 string `json:"field3" example:"Field3"`
	Field4 string `json:"field4" example:"Field4"`
}

// GetData2 godoc
//
//	@Summary		Get data2
//	@Produce		json
//	@Success		200				{object}	Response{data=GetData2Response}
//	@Router			/internal/data2 [get]
func GetData2(w http.ResponseWriter, r *http.Request) {
	//write your code
}

Expected behavior
expect swagger.yaml should be like this
image

not currently would got like this
image

Your swag version
v2.0.0-rc4

Your go version
go1.23.1

Desktop (please complete the following information):

  • OS: macOS, darwin/arm64
  • Browser: arc
  • Version: Sonoma Version 14.5

Additional context
Add any other context about the problem here.

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

1 participant