swag
swag copied to clipboard
Support embedded struct with json tag
Is your feature request related to a problem? Please describe. Currently If you have an embded struct with a json tag, the json tag is ignored by swaggo. When marshaling to json normally it is not.
package main
import "encoding/json"
import "fmt"
type A struct {
Foo string
}
type B struct {
A `json:"A"`
Bar string
}
func main() {
b, _ := json.Marshal(B{})
fmt.Println(string(b))
}
output (should also be the expected result for swaggo):
{"A":{"Foo":""},"Bar":""}
Currently with swaggo generation the result would be
{"Foo":"", "Bar":""}
which is not expected
Describe the solution you'd like When using a custom type that has a embeded struct it should still read the json tag and generate the definition the same as my expected result above.
Have you tried ?
type B struct {
Alias A `json:"A"`
Bar string
}
Unfortunately I can't in my situation. When using sqlx the struct scanning doesn't handle nested structs it uses embedded instead. Regardless the result of the standard library json marshal is different from what swaggo is doing.
I have the same problem. Is there currently any way to solve this? If there's not, any thoughts on how can this be implemented? I'd be happy to contribute :)
Anyway, I think, swaggo has to respect the standard library json
I just came across same problem.
Using velero, structs have defined metadata as embeded, but as field in json - https://github.com/vmware-tanzu/velero/blob/48d185985ab044a46dc91864a2fbd1f6430f56ee/pkg/apis/velero/v1/backup.go#L339
So it results as embeded in swagger docs, but as field in json, which is huge problem.
Same problem here + an extra one.
Problem 1. Embedded structs are flattened out in the parent struct in generated docs, even if in the real response they are not
Embedded structs (required when working with sql(x)):
type Address struct {
ID uint64 `db:"id"`
StreetAddress string `db:"street_address"`
ZipCode string `db:"zip_code"`
City string `db:"city"`
Region string `db:"region"`
Country string `db:"country"`
TimeZoneID string `db:"time_zone_id"`
CreatedOn time.Time `db:"created_on"`
UpdatedOn time.Time `db:"updated_on"`
Version uint16 `db:"version"`
}
type Organization struct {
Address `db:"address" json:"Address"`
ID uint64 `db:"id"`
Name string `db:"name"`
CreatedOn time.Time `db:"created_on"`
UpdatedOn time.Time `db:"updated_on"`
Version uint16 `db:"version"`
}
How they look in the generated docs

How they look in the actual HTTP response

Problem 2. Struct fields in Go and JSON are in PascalCase, but they are camelCase in the generated docs.
ℹ️ There are no json:"fieldName"
tags defined, except for the embedded Address
- i.e. json:"Address"
, which is completely ignored, anyway, since everything is flattened as described above.
I submitted a PR for this, please test :)
Note that there is a panic in the latest version (#1392) so you may have to build from the previous release instead https://github.com/jgillich/swag/tree/prev