Skip to content

Java support for discriminator attribute when reading objects #1253

Closed

Description

@cknowles

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:

public class StepDeserializer extends JsonDeserializer<Step> {

    public StepDeserializer() {
        super();
    }

    @Override
    public Step deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        String stepType = node.get("step_type").asText();
        StepTypeEnum stepTypeEnum = StepTypeEnum.valueOf(stepType);

        Class<? extends Step> valueType = null;

        switch (stepTypeEnum) {
            case condition:
                valueType = ConditionStep.class;
                break;
            case pause:
                valueType = PauseStep.class;
                break;
            case request:
                valueType = RequestStep.class;
                break;
        }

        if (valueType != null) {
            return mapper.treeToValue(node, valueType);
        } else {
            throw new JsonParseException("Enumeration value of " + stepType + " not recognised",
                    jp.getCurrentLocation());
        }
    }

}

Then this JsonDeserializer can be added to the generation in two ways I think.

Either to the static initialization in JsonUtil:

SimpleModule module = new SimpleModule();
module.addDeserializer(Step.class, new StepDeserializer());
mapper.registerModule(module);

Or to Step itself (this gives stack overflow, need to work out how to stop subclasses from using the same deserializer perhaps):

@ApiModel(description = "")
@JsonDeserialize(using = StepDeserializer.class)
public class Step  {

Option 2

These annotations on Step itself:

@JsonTypeInfo(use = Id.CUSTOM, property = "step_type", include = As.PROPERTY)
@JsonTypeIdResolver(StepTypeIdResolver.class)

And then this custom type resolver, can't see a way to do it with the built in resolvers:

public class StepTypeIdResolver implements TypeIdResolver {

    private JavaType mBaseType;

    @Override
    public void init(JavaType baseType) {
        mBaseType = baseType;
    }

    @Override
    public String idFromValue(Object value) {
        return idFromValueAndType(value, value.getClass());
    }

    @Override
    public String idFromValueAndType(Object value, Class<?> suggestedType) {
        if (RequestStep.class.equals(suggestedType)) {
            return Step.StepTypeEnum.request.toString();
        } else if (ConditionStep.class.equals(suggestedType)) {
            return Step.StepTypeEnum.condition.toString();
        } else if (PauseStep.class.equals(suggestedType)) {
            return Step.StepTypeEnum.pause.toString();
        }
        return String.valueOf(value);
    }

    @Override
    public String idFromBaseType() {
        return idFromValueAndType(null, mBaseType.getRawClass());
    }

    @Override
    public JavaType typeFromId(String id) {
        Class<? extends Step> clazz = null;
        if (StepTypeEnum.request.toString().equals(id)) {
            clazz = RequestStep.class;
        } else if (StepTypeEnum.condition.toString().equals(id)) {
            clazz = ConditionStep.class;
        } else if (StepTypeEnum.pause.toString().equals(id)) {
            clazz = PauseStep.class;
        }
        return TypeFactory.defaultInstance().constructSpecializedType(mBaseType, clazz);
    }

    @Override
    public JsonTypeInfo.Id getMechanism() {
        return Id.CUSTOM;
    }

}

Activity

  1. cknowles commented on Sep 28, 2015

    @cknowles
    Author

    I started on some of the code for this, turns out the discriminator isn't supported at all right now in the codegen project.

  2. fabiankessler commented on Oct 23, 2015

    @fabiankessler

    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")
    @JsonSubTypes({@JsonSubTypes.Type(value = MySubClassOne.class, name = "MySubClassOne")})

    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).

  3. added this to the v2.1.5 milestone on Oct 26, 2015
  4. modified the milestones: Future, v2.1.5 on Jan 13, 2016
  5. modified the milestones: Future, v2.3.0 on Jul 7, 2016
  6. modified the milestones: v2.2.1, v2.2.2 on Aug 8, 2016
  7. manuc66 commented on Feb 1, 2017

    @manuc66
    Contributor

    It seems that this pull request : #4607 will solve this with @JsonTypeInfo and @JsonSubTypes

  8. wing328 commented on Feb 3, 2017

    @wing328
    Contributor

    #4607 has been merged into master. Thanks for the contribution from @eamon316

    Please pull the latest master to give it a try.

  9. cbornet commented on Feb 3, 2017

    @cbornet
    Contributor

    #4607 is for Spring. Has it also been done for Jackson based java clients ?

  10. wing328 commented on Feb 3, 2017

    @wing328
    Contributor

    @cbornet yes, you're right. I got confused :(

  11. jeff9finger commented on Feb 3, 2017

    @jeff9finger

    This looks very similar to #4085 and #4226

  12. cbornet commented on Feb 3, 2017

    @cbornet
    Contributor

    @jeff9finger yes, so java clients now also support it, it seems.

  13. jeff9finger commented on Feb 3, 2017

    @jeff9finger

    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")
    @JsonSubTypes({@JsonSubTypes.Type(value = MySubClassOne.class, name = "my-sub-class-one")})

    definitions:
    MySubClassOne:
    x-discriminator-value: my-sub-class-one
    ...

  14. jeff9finger commented on Feb 3, 2017

    @jeff9finger

    It should work on any jaxrs models, server or client

  15. wing328 commented on Feb 15, 2017

    @wing328
    Contributor

    @c-knowles please pull the latest master to give it another try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions