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
subtypes & discriminator would benefit from more info #44
Comments
I also vote up this issue. Working with inheritance at the model and have to make some fixes in swagger core and swagger ui because of that. It seems that no one have used this feature before (or failed to use because of some stoppers) |
+1. ex. in Cat example, when accessing to Cat.type, it's value should be the value of Cat.id, which is 'Cat'. Am I on the right track? |
The way I read the spec is the same as what @mission-liao says. I think this is a bit limiting to constrain the discriminator values to the model id. Perhaps if there were an optional "discriminatorValue" attribute on subtypes that controlled the value discriminator could take to signal that the data is an instance of that subtype.? This would be closer what Jackson is doing which is an annotation that looks like: @JsonSubTypes({ @type(value = Cat.class, name = "cat-discriminator"), @type(value = Dog.class, name = "dog-discriminator") }) |
The current composition model looks like such: {
"Pet": {
"discriminator": "petType",
"properties": {
"name": {
"type": "string"
},
"petType": {
"type": "string"
}
},
"required": [ "name", "petType" ]
}
} From this definition, the consumer will know that somewhere we need to tell the client what class to instantiate. We would either need to list all the subtypes in the parent class--along with the discriminator. In the 1.2 spec, that was very ugly and frowned upon. What would a better structrue be? |
I don't think we need to do anything. It's implicit. Any schema that uses |
@fehguy - Is this still relevant? |
I'm wondering how to structure API schemas that use polymorphism and not to put all of them in swagger.json. Specifying external schema urls in {
"name": "Felix",
"petType": "http://example.org/schemas/cat.json"
} That would be understandable for clients as well, even without building a tree of definitions. What do you think? Could such values be allowed? Downside is that client won't be able to preload schemas. Another approach is to mention them in definitions and use {"definitions": {
"cat": {
"$ref": "http://example.org/schemas/cat.json"
}
}} |
Closing it as it's related to 1.2. 2.0 changes things a bit, and we'll tackle the topic in the related modeling issue. |
Could you provide the link to the related modelling issue? |
That still doesn't tell me which of those schemas now is the one to choose, or do I miss something? Ah, here:
So this discriminator property must match the name which is used as key in the definitions object for defining the schema (and inline schemas can't be subtypes). I guess the original request by @fehguy was to make this mapping a bit more flexible by not linking the schema keys (which otherwise can be swapped out without changing anything in the API, as long as the refs are changed too) directly to the on-the wire content. |
@ePaul if you can remember full intent of comments you've made a year and a half ago, I salute you ;) |
I realize that this issue is closed but what was the resolution here? Do the values of the discriminator field have to exactly match the name of the subType? Otherwise, how does the client know specifically which value of the discriminator generates which subtype? |
Swagger 1.2 lets you specify a discriminator and possible subtypes, but it doesn't relate the
value
of the discriminator and the model that should be instantiated. It would be convenient if this could be expressed in the spec.The text was updated successfully, but these errors were encountered: