-
Notifications
You must be signed in to change notification settings - Fork 135
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
Enum types should generate a schema that includes the possible enum values #57
Comments
Yes, I can reproduce this with 2.5.1-SNAPSHOT. Not sure what gives; |
Actually, hmmh. Not sure I can reproduc this. Are you using the latest versions of databind and JSON Schema module? 2.4.4 actually had one fix that may be relevant here; so either 2.4.4 or 2.5.0 would be versions to test. I modified an existing test to ensure that |
Specifically, what needs to happen is enums should be encoded (I think starting in java 1.6) using an array of all the enum values via ENUMVALUE.toString(). I believe prior to 1.6 it would need to be via ENUMVALUE.name(), though I would have to do some research to confirm. I'm using java 8 so an array of all the ENUMVALUE.toString()s would be perfect |
@cowtowncoder Can you reproduce using the MyClass class that I included in the bug report? I'm still seeing it with jackson 2.5.0
|
Jackson default method for serializing So: what test needs to demonstrate is that JSON Schema includes values different from serialization. Put another way: Jackson should produce Schema that is compatible with its own serialization method. |
Ok so I just cloned jackson-databind and attempted to duplicate by modifying TestGenerateJsonSchema's SimpleBean class to have a simple enum with values and tested the schema validation and that, oddly enough, generated exactly the expected output. However, if I plop my own little class down in that package and write a new test based on that class, the proper enum schema is NOT generated. Still digging to see if I can figure out what is special about my enum that is breaking it |
Found it.
Then the schema generates as expected. |
Ok, I have a test you can use to reproduce. Modify the following file: Add the following nested class:
Then add the following test:
With the @JsonValue annotation present the test will fail, with it commented out it will pass |
Ok, just to make sure: it looks like |
Not sure if I read you right, so I'll rehash it just to make sure we're saying the same thing:
|
No, I don't think this is how I see it. To me, JSON Schema generation should produce schema that validates actual output serializer would do. Why would definition of valid for schema be different from actual produced output? Keep in mind that JSON Schema knows nothing about datatypes or objects, and only about actual literal JSON serialization -- enumerated Strings that may be found in instance documents. |
I think I see a communication disconnect here. (Hi there, I work with @StormeHawke.) If my enum has:
I expect the json schema to be: If instead I have:
I expect my json schema to be: In this way, I expect So in sum: |
That explains it perfectly. |
Ah! Ok, I did miss some parts. I agree here, I think. Thank you for clarifying this! One thing that would help here is a simple unit test: I will eventually get to it and should be able to write one. But a contribution could speed things up. |
Ah-ha. I can reproduce this, and the root problem is that |
Ok. This will get rather tricky to fix, but is probably doable. Requires special handling of |
+1 I also have this issue using Jackson version 2.5.1 |
Ok. Needed a brutal hack in |
Hmmh. Looking back at this, I am actually not sure this is not something to support, because meaning of But given that this is the current behavior I guess it'll have to do. Just noticed the problem when rewriting schema traversal for |
Cross posting from my StackOverflow question. After doing some digging it seems like the schema is defaulting to using the bare serialized type rather than properly using the possible enum values.
Link to the question on StackOverflow:
http://stackoverflow.com/questions/27863689/generate-json-schema-from-pojo-with-a-twist
I'm generating a JSON schema from a pojo. My code to generate the schema looks like so:
I'm generating several schemas via the above code. One of the pojos has an internal embedded enum to limit the possible values, like so:
The above code should limit the possible String values in the JSON data that's passed around to "Monday", "Tuesday", "Wednesday", etc.
When I run the schema generator on the code in question, I expect to get something like the following schema:
but instead I'm getting this:
I've done some digging in the Jackson Schema Module source code and figured out that what's happening is Jackson's using ".toString()" as the default serialization method for enum types, but what I need it to do instead is create the line that looks like this based on
StartDayOfWeek.values()
:The text was updated successfully, but these errors were encountered: