Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Using @XmlEnum to generate an enum of type Integer in json schema #33

Closed
jesse8888 opened this issue Oct 31, 2014 · 9 comments
Closed
Milestone

Comments

@jesse8888
Copy link

I just posted an issue in jackson-module-jsonSchema.

FasterXML/jackson-module-jsonSchema#49

For completeness:

I have the following Java enum:

@XmlType
@XmlEnum(Integer.class)
public enum DefaultMessageVersion
{
    @XmlEnumValue("1") ONE;
}

That is producing the following json schema snippet:

"defaultMessageVersion" : {
"type" : "string",
"enum" : [ "1" ]
}

This is because (I believe) Jackson ignores the @XmlEnum annotation. I would like the outputted snippet to have "type" : "integer" (or int? I'm not 100% sure which it is).

I have posted the issue here at the request of cowtowncoder, and am hoping that I can submit a fix myself (with pointers on where to look), or that someone else with more knowledge and expertise can take care of it without too much trouble.

@cowtowncoder
Copy link
Member

Implemented so that annotation like this will have same semantics as:

@JsonFormat(shape=Shape.NUMBER_INT)

which for JSON will mean that enum will actually be written as numbers (using index).
And JSON Schema generator, in turn, should product proper type. However, testing is next needed on schema generator to see if things work end-to-end.

@jesse8888
Copy link
Author

I am testing this in my environment (with the latest code pulled in) and am still seeing the same output schema

"defaultMessageVersion" : {
"type" : "string",
"enum" : [ "1" ]
}

My apologies, but I am unfamiliar with the process here. Have you confirmed that your implementation works end to end, or is testing still needed?

@cowtowncoder
Copy link
Member

I haven't looked at JSON Schema part, just JAXB handling thereof (and usage from main serializer).
I'll check schema generation next, to see how enums are actually handled.

cowtowncoder added a commit to FasterXML/jackson-databind that referenced this issue Oct 31, 2014
@jesse8888
Copy link
Author

Thanks! I’m happy to help you test anything you implement.

@cowtowncoder
Copy link
Member

Ok: two additional notes;

  1. Make sure dependencies via Maven are correct; fix will be in 2.4.4(-SNAPSHOT) and 2.5.0
  2. One minor problem existed in EnumSerializer of jackson-databind, regarding detection of "serialize
    using index", which I just fixed.

With these, JSON Schema generation should reflect the annotation.

Beyond this, I am not sure if listing of enumerated values should or should not be included.
At the moment it won't be, but it'd be possible to add.

@jesse8888
Copy link
Author

Thanks for the dependencies info; I'll make sure I'm pulling in the right versions when I test.

My vote is that the listing of the enumerated values should be included.

@cowtowncoder
Copy link
Member

That's sort of up JSON Schema spec: is number of enumerated values allowed for int types?
I can try to see if I can decode this aspect.

@kwinter
Copy link

kwinter commented May 7, 2015

What should the outcome of including @XmlEnumValue be? We have the following:

@XmlEnum(Integer.class)
public enum RecommendationType
{
  @XmlEnumValue("1")
  SUGGESTED_CANDIDATES(1),
  @XmlEnumValue("2")
  SUGGESTED_RECRUITERS(2),
  ...

prior to this change, the result would have been "1" or "2." With this change, it is now 0 or 1. I've taken out the Integer.class from our code, but it was a bit of a sneaky gotcha when upgrading Jackson. Ideally I would think that we'd want the result to be 1 or 2, without the quotes?

@cowtowncoder
Copy link
Member

@kwinter I am guessing this is due to combination of things, and specifically indication that Enum is to be serialized as a number suggests to Jackson that the index should be used, and textual value ignored. Jackson does not have a way to indicate numeric value override for enums.

I think the handling is not optimal, and it is unfortunate that behavior changed for existing code.
But I am not hopeful that this would be easy to resolve due to combination of pieces involved: converting from JAXB annotations to Jackson's internal types, and then passing that to JSON Schema generator.
So I think that the easiest work-around is unfortunately removal of @XmlEnum indicator, just like you have done.

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

No branches or pull requests

3 participants