swag
swag copied to clipboard
How do you annotate Request containing array of objects potentially having different data type(struct)?
Illustration or sample JSON I would like to support: { streams[]: { entityType: 1, f1:"foobar", f2:"hello", }, { entityType: 1, f1:"foo baz", f2:"hello 2", }, { entityType: 2, f1:"sound", s1:123, } { entityType: 3, f1:"picture2", t1:1.23, } }
The closest Golang struct definition for above are: type A struct { EntityType int json:"entityType" F1 string json:"f1" F2 string json:"f2" } structs B & C is similar to A, see the JSON above for entityType 2 & 3 for the field definitions.
Perhaps I have another struct so I can create a top level field named "streams":
type StreamsRequest struct{
Streams []interface{} json:"streams"
}
How do you annotate that so swag can generate swagger spec from the code?
Same question for me.
@grecinto In most common cases each endpoint will have its own Request/Response pair.
type Stream struct {
EntityType int `json:"entityType"`
F1 string `json:"f1,omitempty"`
F2 string `json:"f2.omitempty"`
I1 int `json:"i1,omitempty"`
}
type StreamsRequest struct{
Streams []Stream `json:"streams"`
}
If you are going to use the interface{} approach, there is a workaround using swaggertype tag that I really don't recommend because will make the code hard to read,
I see, you are suggesting to create a Super struct that contains all fields.
omitempty those “differing” ones. Let me see… but this struct will look Frankensteinish. :)
I wanted to avoid creating a struct just for spec generation.
Perhaps []interface approach, pls share? Fyi, the code already knows how to read incoming. I am just trying to annotate to generate Spec from code.
I already gave you the link that uses swagger type
https://github.com/swaggo/swag/blob/master/example/object-map-example/controller/response.go
@grecinto any updates?
This is helpful https://github.com/swaggo/swag/blob/master/example/object-map-example/controller/response.go but I want to achieve sth like this:
type Usage struct {
Offers []Offer`json:"offers,omitempty" swaggertype:"array,object,string" example:"[{key:value,key2:value2}]"`
}
so that the output will be
"offers": [
{
"key": "value",
"key2": "value2"
}
]
This gives me an error :object is unsupported type in example value [{key:value
Is there a way that I can achieve this?
any update of this?
@wihlarkop any updates for what? :smile:
This is helpful https://github.com/swaggo/swag/blob/master/example/object-map-example/controller/response.go but I want to achieve sth like this:
type Usage struct { Offers []Offer`json:"offers,omitempty" swaggertype:"array,object,string" example:"[{key:value,key2:value2}]"` }so that the output will be
"offers": [ { "key": "value", "key2": "value2" } ]This gives me an error
:object is unsupported type in example value [{key:valueIs there a way that I can achieve this?
Any update? I need this too.
This seems like a nice feature to have! I would need this too if it's possible.
JSON arrays are not supported yet. I came across the same issue, any update?