go-swagger / go-swagger

Swagger 2.0 implementation for go
https://goswagger.io
Apache License 2.0
9.57k stars 1.26k forks source link

Client cannot work for polymorphic model when server side is upgraded #1757

Open alexz0000 opened 6 years ago

alexz0000 commented 6 years ago

Problem statement

I can see the generated code for polymorphic model in client. And the unmarshal func will validate the discriminator. For example

return nil, errors.New(422, "invalid name value: %q", getType.Name)

My question is what if the server side add new sub-models? what if the user doesn't upgrade the client SDK? Looks the their old SDK will get panic.

I didn't find a solution, so create this request. Could you give me a solution? Thanks.

Swagger specification

Swagger 2.0

Steps to reproduce

Environment

swagger version: 0.15.0 go version: 1.10.3 OS: Mac OS

casualjim commented 6 years ago

it would fail yes, but since the spec is upgraded you're effectively working with a new version of the server api, in that case the client does need to regenerate. But at least you have defined behavior instead of undefined behavior.

alexz0000 commented 6 years ago

@casualjim But users/customers may not be want to upgrade their SDK, If we force they do this, they may not be happy. And also, since we have lots of polymorphic APIs, some of them are upgraded very frequently, If they get failed frequently, I don't think they are happy. Actually, I prefer to ignore the new added sub-models. If the users/customers want to use the new added models, they can decide if upgrade to latest version of SDK

casualjim commented 6 years ago

One solution can also be to implement a default implementation of the polymorphic base type and return an instance of that. At least you'd get basic functionality.

the files to change are these: https://github.com/go-swagger/go-swagger/blob/master/generator/templates/schema.gotmpl https://github.com/go-swagger/go-swagger/blob/master/generator/templates/schemabody.gotmpl

alexz0000 commented 6 years ago

Thanks, I will try it.

alexz0000 commented 6 years ago

When I tried to update the schema template, I got below error message. Looks I cannot override the templates except I update the code of go-swagger. If there any ways to let it effect?

Could not complete template processing in directory "../go-swagger-local/templates": Could not add template: Cannot overwrite protected template schema
fredbi commented 6 years ago

generate, modify then reuse model with x-go-type

alexz0000 commented 6 years ago

I think the solution cannot get the purpose. And we're gonna to force our customers to upgrade their SDK version if there are new sub-models.