swag icon indicating copy to clipboard operation
swag copied to clipboard

How do you annotate Request containing array of objects potentially having different data type(struct)?

Open grecinto opened this issue 3 years ago • 8 comments

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?

grecinto avatar Dec 08 '21 20:12 grecinto

Same question for me.

imkerberos avatar Dec 09 '21 01:12 imkerberos

@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,

ubogdan avatar Dec 15 '21 12:12 ubogdan

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.

grecinto avatar Dec 15 '21 16:12 grecinto

I already gave you the link that uses swagger type

https://github.com/swaggo/swag/blob/master/example/object-map-example/controller/response.go

ubogdan avatar Dec 15 '21 18:12 ubogdan

@grecinto any updates?

ubogdan avatar Jan 01 '22 13:01 ubogdan

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?

stcharitak avatar Feb 24 '22 13:02 stcharitak

any update of this?

wihlarkop avatar Jun 15 '22 11:06 wihlarkop

@wihlarkop any updates for what? :smile:

ubogdan avatar Jun 16 '22 09:06 ubogdan

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? I need this too.

tirzasrwn avatar Feb 01 '23 03:02 tirzasrwn

This seems like a nice feature to have! I would need this too if it's possible.

npitsillos avatar Apr 27 '23 10:04 npitsillos

JSON arrays are not supported yet. I came across the same issue, any update?

joseluisq avatar May 02 '23 08:05 joseluisq