Open sidh opened 5 years ago
Could you please post the generated model and point out what is wrong with it? Thanks.
The desired type is map[string]json.RawMessage.
For the first case it generates map[string]interface{}.
For the second case it generates map[string]MyTypeAnon with
type MyTypeAnon struct {
json.RawMessage
}
First case looks like a bug. Second case is expected, since the allOf
makes a new type.
Agree on the second case. First case is probably a bug, yes.
Also it seems x-go-type does not work with properties of an object:
MyType:
type: object
properties:
prop:
type: object
x-go-type:
import:
alias: "json"
package: "encoding/json"
type: "RawMessage"
results in
type MyType struct {
Prop interface{}
}
instead of
type MyType struct {
Prop json.RawMessage
}
Yes. I just checked in here: x-go-type extension is currently only accounted for definitions, not nested structures. So this is not a bug, but a (limited) feature... :)
I don't think this feature was created in the first place for this use case of hijacking type mappings inside definitions.
A hacky way to go could be to change the type mapping, like defined here and define or redefine some format to map as json.RawMessage
.
A hacky way to go could be to change the type mapping, like defined here and define or redefine some format to map as
json.RawMessage
.
This solution will work too. May be thats the correct way to go?
it is not supported atm but you may still experiment locally based on this. I am not sure about how we could integrate this with a PR while making sure it is not breaking anything.
Some days ago, I was reflecting on a similar problem about optionally mapping binary
format as []byte
rather than the default io.ReadCloser
. I feel this is a related theme.
Some days ago, I was reflecting on a similar problem about optionally mapping
binary
format as[]byte
rather than the defaultio.ReadCloser
. I feel this is a related theme.
We can never do that because you lose the ability to support a very important use case. You can easily go from slice to stream but you can't do that the other way around and guarantee the code will still function
@sidh you can use format: byte
for a string, that's the closest you can get to what you want to do
Or make a type with a custom marshaler
I initially wanted to dynamically handle part of JSON and for that I need json.RawMessage
as simple format:byte
will expect just bytes, not real JSON.
Related to #1622.
I ran into the same problem, as a work around this seems to work:
definitions:
event:
type: object
properties:
id:
type: string
data:
ref: '#/definitions/data'
data:
type: object
x-go-type:
import:
alias: "json"
package: "encoding/json"
type: "RawMessage"
and this generates the following code:
// Event event
// swagger:model event
type Event struct {
// data
Data json.RawMessage `json:"data,omitempty"`
// id
// Required: true
ID *string `json:"id"`
What is the correct way to generate map[string]json.RawMessage model?
x-go-type does not work in this case:
but does work in this
but generates an unneeded type that holds json.RawMessage.