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
Server cannot unmarshal array of polymorphic values #1637
Comments
You're using a really old version. Please try with the latest release, this should be fixed. |
Just upgraded to 0.15.0. Same result :/ |
I guess it's different because of this: When payload is to be an object that contains the array instead of just the array, it works. |
Could you please add here the BindRequest() method generated for your operation? |
Sure thing. Here you go: // BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
// for simple values it will use straight method calls.
//
// To ensure default values, the struct must have been initialized with NewTestParams() beforehand.
func (o *TestParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
var res []error
o.HTTPRequest = r
if runtime.HasBody(r) {
defer r.Body.Close()
var body []models.Value
if err := route.Consumer.Consume(r.Body, &body); err != nil {
if err == io.EOF {
res = append(res, errors.Required("payload", "body"))
} else {
res = append(res, errors.NewParseError("payload", "body", "", err))
}
} else {
// validate array of body objects
for i := range body {
if err := body[i].Validate(route.Formats); err != nil {
res = append(res, err)
break
}
}
if len(res) == 0 {
o.Payload = body
}
}
} else {
res = append(res, errors.Required("payload", "body"))
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
} |
Yes I've seen already this when trying to fix some other stuff with polymorphism. |
I did not release this fix, because there are many other problems here. |
Well, if this cannot be fixed for all possible cases it should at least be documented as a limitation of go-swagger, don't you think? Not sure, how heavily used this feature is anyway. |
i am not saying this cannot be fixed. I am saying it is not fixed yet because it is not a simplistic issue. |
You got me there. I definitely have to catch up on the docs. |
Found minimal patch to make this one work. There are many other edge cases with polymorphism. |
…body parameter) > NOTE: polymorphism still gets a number of uncovered edge cases. > This provides minimal patching for this particular case.
Problem statement
Not sure if this is related to #842, but when an endpoint expects an array of polymorphic values, the server fails with the following error message:
Swagger specification
Steps to reproduce
Environment
swagger version: 0.12.0
go version: go1.9.1 darwin/amd64
OS: MacOS but should be irrelevant
Workaround
When
payload
is to be an object that contains the array instead of just the array, it works.Since the issue can be worked around it is not a blocking issue for me, I am submitting this issue more out of curiosity as to what provokes this behavior.
Edit: fix invalid sample spec
The text was updated successfully, but these errors were encountered: