-
Notifications
You must be signed in to change notification settings - Fork 73
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
struct with json.Marshaler fail to build schema #79
Comments
thank you reporting this. Could you help me by providing a testcase for this? |
is there some examples for adding a test cases |
here is the test type Parent struct {
FieldA string
}
func (v Parent) MarshalJSON() ([]byte, error) { return nil,nil }
type Child struct {
FieldA Parent
FieldB int
}
func TestParentChildArray(t *testing.T) {
db := definitionBuilder{Definitions: spec.Definitions{}, Config: Config{}}
db.addModelFrom(Child{})
s := spec.Schema{
SchemaProps: spec.SchemaProps{
Definitions: db.Definitions,
},
}
data, _ := json.MarshalIndent(s, "", " ")
log.Fatalln(string(data))
} output: {
"definitions": {
"restfulspec.Child": {
"required": [
"FieldA",
"FieldB"
],
"properties": {
"FieldA": {
"type": "string"
},
"FieldB": {
"type": "integer",
"format": "int32"
}
}
}
}
} |
thank you for the test, I will look for a solution |
***@***.***:377636000地址:浙江省杭州市西湖区浙江大学西溪校区
|
@emicklei have you considered simply deleting the code block checking for the custom marhalling? What is it for anyway? Found this because I was having a similar (the same?) problem. I have a type // ValidJSONArray represents a generic slice which, when marshalled to json, will give an empty array
// for both nil and empty slices. This prevents a field which a client expects to be an array from being "null"
type ValidJSONArray[T any] []T
func (v ValidJSONArray[T]) MarshalJSON() ([]byte, error) {
if v == nil {
v = make([]T, 0)
}
return json.Marshal([]T(v))
} and when I replaced my slice properties with ValidJSONArray, suddently they all had type of "string" in the open api spec! e.g. this produces a correct spec:
but this does not
however, if I just delete the code block in the screenshot in the original post on this issue, it works exactly as expected. |
***@***.***:377636000地址:浙江省杭州市西湖区浙江大学西溪校区
|
so running the last test with code that skips checking the json Marshalling, I get:
|
the code of https://github.com/emicklei/go-restful-openapi/ is here:
test struct
open-api json struct
The text was updated successfully, but these errors were encountered: