You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello!
We are using swagger with swag to generate the docs for our golang API. We are struggling to get the correct marshal for the body
structure when using a struct that we defined with generics.
We have a main struct for our request body received on our endpoints. This main struc (Myrequest) has a Transaction field
of type RequestTransaction that accepts a generic type.
RequestTransaction has 2 fields: Info (another struct) and Data (which can be any type). We created this interface RequestData
to state the accepted data types, and the implementation is given bellow.
Problem: when we use swag to provide an example of the request body, it generates an empty json because of the generic types.
The swagger only displays {}
This is the code implementation:
// RequestData is an interface that specifies all data variations the request body may have type RequestData interface { AData | BData | CData }
// MyRequest is a structure that represents the received payload, this can be
// different depending on the underlying data type. type MyRequest[T RequestData] struct { Transaction RequestTransaction[T]json:"transaction_field" }
// RequestTransaction contains Info and Data fields. type RequestTransaction[T RequestData] struct { Info SomeSimpleStruct json:"info" Data T json:"data" }
//Info field is not an issue. The Data field is...
//We define the Request types based on the data types, like this:
// AData request type alias to MyRequest[AData]. type ADataRequest = MyRequest[AData]
// BData request type alias to MyRequest[BData]. type BDataRequest = MyRequest[BData]
//Then, we define the Transaction type like this
// ADataTransaction type alias to RequestTransaction[AData]. type ADataTransaction = RequestTransaction[AData]
// BDataTransaction type alias to RequestTransaction[BData]. type BDataTransaction = RequestTransaction[BData]
//AData, BData, CData are simple structs containing the fields required for each type.
The problem is that I'm not able to generate an example of the body type using Swag because when I use this syntax: // @Param request body ADataRequest true "A data request"
It shows an empty json.
I've tried this approches without success. I tried:
First approach - Overriding the fields on the structure with the concrete types like this:
// @Param request body ADataRequest{Transaction=ADataTransaction{Data=AData, Info=SomeSimpleStruct}} true "A data request"
This almost works except for the fact that it cannot marshal the json fields transaction_field, data and info and shows the result like this:
{
"Transaction": {
"Data": {
"field1": "something",
"field2": "OK",
"field3": "2023-03-07T01:00:00.472Z"
},
"Info": {
"field_a": "a",
"field_b": "b",
"field_c": "c"
}
}
}
Second approcah - Tried to use the SchemaExample() using a string with the example on it: // @Param request body string true "A data type" SchemaExample({\n "transaction_field": {\n "data": {\n "field1": "something",\n "field2": "OK",\n "field3": "2023-03-07T01:00:00.472Z"\n },\n "info": {\n "field_a": "a",\n "field_b": "b",\n "field_c": "c"\n }\n }\n})
Can you help me in order to get the correct body example on swagger, please? I think that might be a way of making the first approach work, but I can't find it.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello!
We are using swagger with swag to generate the docs for our golang API. We are struggling to get the correct marshal for the body
structure when using a struct that we defined with generics.
We have a main struct for our request body received on our endpoints. This main struc (Myrequest) has a Transaction field
of type RequestTransaction that accepts a generic type.
RequestTransaction has 2 fields: Info (another struct) and Data (which can be any type). We created this interface RequestData
to state the accepted data types, and the implementation is given bellow.
Problem: when we use swag to provide an example of the request body, it generates an empty json because of the generic types.
The swagger only displays {}
This is the code implementation:
// RequestData is an interface that specifies all data variations the request body may have
type RequestData interface { AData | BData | CData }
// MyRequest is a structure that represents the received payload, this can be
// different depending on the underlying data type.
type MyRequest[T RequestData] struct { Transaction RequestTransaction[T]
json:"transaction_field"}
// RequestTransaction contains Info and Data fields.
type RequestTransaction[T RequestData] struct { Info SomeSimpleStruct
json:"info"Data T
json:"data"}
//Info field is not an issue. The Data field is...
//We define the Request types based on the data types, like this:
// AData request type alias to MyRequest[AData].
type ADataRequest = MyRequest[AData]
// BData request type alias to MyRequest[BData].
type BDataRequest = MyRequest[BData]
//Then, we define the Transaction type like this
// ADataTransaction type alias to RequestTransaction[AData].
type ADataTransaction = RequestTransaction[AData]
// BDataTransaction type alias to RequestTransaction[BData].
type BDataTransaction = RequestTransaction[BData]
//AData, BData, CData are simple structs containing the fields required for each type.
The problem is that I'm not able to generate an example of the body type using Swag because when I use this syntax:
// @Param request body ADataRequest true "A data request"
It shows an empty json.
I've tried this approches without success. I tried:
First approach - Overriding the fields on the structure with the concrete types like this:
// @Param request body ADataRequest{Transaction=ADataTransaction{Data=AData, Info=SomeSimpleStruct}} true "A data request"
This almost works except for the fact that it cannot marshal the json fields transaction_field, data and info and shows the result like this:
{
"Transaction": {
"Data": {
"field1": "something",
"field2": "OK",
"field3": "2023-03-07T01:00:00.472Z"
},
"Info": {
"field_a": "a",
"field_b": "b",
"field_c": "c"
}
}
}
Instead of this:
{
"transaction_field": {
"data": {
"field1": "something",
"field2": "OK",
"field3": "2023-03-07T01:00:00.472Z"
},
"info": {
"field_a": "a",
"field_b": "b",
"field_c": "c"
}
}
}
Second approcah - Tried to use the SchemaExample() using a string with the example on it:
// @Param request body string true "A data type" SchemaExample(
{\n "transaction_field": {\n "data": {\n "field1": "something",\n "field2": "OK",\n "field3": "2023-03-07T01:00:00.472Z"\n },\n "info": {\n "field_a": "a",\n "field_b": "b",\n "field_c": "c"\n }\n }\n})
But the result is like this:
{
"transaction_request": {
"data": {
"field1": "something",
"field2": "OK",
"field3": "2023-03-07T01:00:00.472Z"
},
"info": {
"field_a": "a",
"field_b": "b",
"field_c": "c"
}
}
}
And it should look like this:
{
"transaction_field": {
"data": {
"field1": "something",
"field2": "OK",
"field3": "2023-03-07T01:00:00.472Z"
},
"info": {
"field_a": "a",
"field_b": "b",
"field_c": "c"
}
}
}
Can you help me in order to get the correct body example on swagger, please? I think that might be a way of making the first approach work, but I can't find it.
Beta Was this translation helpful? Give feedback.
All reactions