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

Support embedded struct with json tag #886

Closed
Harrison-Miller opened this issue Feb 16, 2021 · 7 comments · Fixed by #1396
Closed

Support embedded struct with json tag #886

Harrison-Miller opened this issue Feb 16, 2021 · 7 comments · Fixed by #1396

Comments

@Harrison-Miller
Copy link

Harrison-Miller commented Feb 16, 2021

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.

@ubogdan
Copy link
Contributor

ubogdan commented Feb 16, 2021

Have you tried ?

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

@Harrison-Miller
Copy link
Author

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.

@stackdumper
Copy link

stackdumper commented Feb 19, 2021

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

@sdghchj
Copy link
Member

sdghchj commented Feb 25, 2021

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

@cache-sk
Copy link

I just came across same problem.

Using velero, structs have defined metadata as embeded, but as field in json - https:/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.

@padurean
Copy link

padurean commented May 12, 2022

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.

@jgillich
Copy link
Contributor

jgillich commented Nov 24, 2022

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:/jgillich/swag/tree/prev

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 a pull request may close this issue.

7 participants