-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Java support for discriminator attribute when reading objects #1253
Comments
I started on some of the code for this, turns out the discriminator isn't supported at all right now in the codegen project. |
To my the generated code work, all I had to do is add these annotations on the base type: @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") My discriminator field is called "type". And of course the instance must then have the "type" field set to "MySubClassOne". Or else just hardcode that value in each subclass. Yes, it would be very useful if codegen would support this advertised feature. It took me a few hours to find my way around the open issues (there are more). |
It seems that this pull request : #4607 will solve this with @JsonTypeInfo and @JsonSubTypes |
#4607 is for Spring. Has it also been done for Jackson based java clients ? |
@cbornet yes, you're right. I got confused :( |
@jeff9finger yes, so java clients now also support it, it seems. |
There is a vendor extension x-discriminator-value that can be used along with the Jackson annotations to specify the discriminator value to be used instead of the object name. @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") definitions: |
It should work on any jaxrs models, server or client |
@c-knowles please pull the latest master to give it another try. |
Currently there doesn't seem to be any support for the swagger v2 discriminator attribute when reading an entity. I think the expectation would be that we first read in the discriminator field, then read the rest of the object based on this field. Not sure where best to put this but have been playing around with some custom JsonDeserializers.
Representing the Test Steps from https://www.runscope.com/docs/api/steps, I have Step/RequestStep/ConditionStep/PauseStep. I found two options so far which are below. Both seem roughly equivalent in terms of complexity and code size.
Option 1
The addition of a custom deserializer:
Then this JsonDeserializer can be added to the generation in two ways I think.
Either to the static initialization in JsonUtil:
Or to Step itself (this gives stack overflow, need to work out how to stop subclasses from using the same deserializer perhaps):
Option 2
These annotations on Step itself:
And then this custom type resolver, can't see a way to do it with the built in resolvers:
The text was updated successfully, but these errors were encountered: