swag icon indicating copy to clipboard operation
swag copied to clipboard

Support embedded struct with json tag

Open Harrison-Miller opened this issue 4 years ago • 6 comments

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.

Harrison-Miller avatar Feb 16 '21 18:02 Harrison-Miller

Have you tried ?

type B struct {
    Alias A `json:"A"`
    Bar string
}

ubogdan avatar Feb 16 '21 21:02 ubogdan

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.

Harrison-Miller avatar Feb 17 '21 04:02 Harrison-Miller

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 :)

stackdumper avatar Feb 19 '21 08:02 stackdumper

Anyway, I think, swaggo has to respect the standard library json

sdghchj avatar Feb 25 '21 06:02 sdghchj

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.

cache-sk avatar Nov 23 '21 16:11 cache-sk

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

image

How they look in the actual HTTP response

image

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.

padurean avatar May 12 '22 12:05 padurean

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

jgillich avatar Nov 24 '22 06:11 jgillich