Skip to content
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] Generator produces a dedicated field for the discriminator #8137

Open
marcelstoer opened this issue May 4, 2018 · 2 comments
Open

Comments

@marcelstoer
Copy link

marcelstoer commented May 4, 2018

Origin: https://stackoverflow.com/q/50163882/131929

Is it correct that the generator produces a dedicated field for the discriminator?

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "errorType", visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = SpecificError.class, name = "SpecificError"),
})
public class GeneralError {
    @JsonProperty("errorType")
    private String errorType = null;
    ...

errorType is the declared discriminator.

When Jackson serializes a SpecificError object it includes errorType twice:

{"errorType":"SpecificError","message":"message","errorType":null}
@marcelstoer marcelstoer changed the title [Java] Generator produces an [Java] Generator produces a dedicated field for the discriminator May 4, 2018
@marcelstoer
Copy link
Author

At FasterXML/jackson-databind#2022 the Jackson team concluded that they can't easily fix this on their end. What would help is to add a @JsonInclude(JsonInclude.Include.NON_NULL) at https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/JavaSpring/pojo.mustache#L28 for all discriminator properties.

@wing328 what is your take on this? Could this even be handled in the template directly or would the generator have to get some extra logic for that?

@VadimKirilchuk
Copy link

VadimKirilchuk commented Feb 17, 2020

@marcelstoer do you really need to have this property in your class? If you don't have one, there is no need in it, it can be solved by visible = "false". So, shouldn't the question be rephrased to "why swagger uses visible = true"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants