Comments (5)
Thanks for reporting this issue @Fischmax.
Could you try using the v1 package and check if you get the same error?
JsonSchemaGenerator is deprecated.
To use the v1 package you have to checkout the project to your local machine and install it.
Let me know if you have any problem.
Thanks!
from jjschema.
Thanks for your fast reply, it is much appreciated!
I just tested with v1 and still get the same error. For a minimal (non-)working example, add
private List<Optional<URI>> uris;
to the example Product
class. I get the following stack trace:
Exception in thread "main" java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class
at com.github.reinert.jjschema.v1.PropertyWrapper.(PropertyWrapper.java:77)
at com.github.reinert.jjschema.v1.CustomSchemaWrapper.processProperties(CustomSchemaWrapper.java:134)
at com.github.reinert.jjschema.v1.CustomSchemaWrapper.(CustomSchemaWrapper.java:60)
at com.github.reinert.jjschema.v1.CustomSchemaWrapper.(CustomSchemaWrapper.java:47)
at com.github.reinert.jjschema.v1.CustomSchemaWrapper.(CustomSchemaWrapper.java:43)
at com.github.reinert.jjschema.v1.SchemaWrapperFactory.createWrapper(SchemaWrapperFactory.java:69)
at com.github.reinert.jjschema.v1.SchemaWrapperFactory.createWrapper(SchemaWrapperFactory.java:45)
at com.github.reinert.jjschema.v1.SchemaWrapperFactory.createWrapper(SchemaWrapperFactory.java:37)
at com.github.reinert.jjschema.v1.JsonSchemaV4Factory.createSchema(JsonSchemaV4Factory.java:36)
at Entry.main(Entry.java:9)
from jjschema.
I checked the issue.
The solution is not trivial...
The issue isn't (only) that the code doesn't handle recursive generics.
It's indeed that the code (1) only handles generics of collections - it should handle generics of any class - and (2) doesn't handle recursive generics.
You can check it in PropertyWrapper class (L73-79).
Unfortunately I do not have the available time to solve it.
I encourage you to go deep in this issue and fix it.
Contributions are always welcome!
from jjschema.
At my workplace, the decision was made to deploy a self-written schema generator instead, which had the same problem which I fixed in a rather ad-hoc way. I am pretty sure I am not allowed to dump the whole thing here, but the important part is:
while (subtype instanceof ParameterizedType) {
if (Iterable.class.isAssignableFrom((Class<?>) ((ParameterizedType) subtype)
.getRawType()))
arrayDepth = arrayDepth + 1;
subtype = ((ParameterizedType) subtype).getActualTypeArguments()[0];
}
As you can see, this merely strips all other generics of the type (we have to deal mostly with wrappers like Optional<> which are not important to us in the JSON) and tracks an additional arrayDepth
field which is later used to wrap the thing in
{
'type':'array',
'items': { … }
}
blocks.
Recursion on the other hand is handled by tracking a list of visited types and inserting a { '$ref': <ID> }
at the appropriate point.
I will ask my employer if I may share the whole thing, depending on if you are even interested: the solution for the generics is probably only useful for us and a more general approach would be nice. But then, you never now what someone is going to do with generics, so it could be hard to anticipate everything.
from jjschema.
I see. Recursion is already solved this way in JJSchema. Nested generics, however, is not handled. I appreciate your tip. Thanks.
from jjschema.
Related Issues (20)
- Json Schema v3? HOT 1
- Dynamic Schema support HOT 2
- java.lang.StringIndexOutOfBoundsException: String index out of range: 0
- ClassCastException when using types that extends Collection<Something>
- java.util.Date serializing and deserializing HOT 1
- Nullable annotation should add null to the list of enums HOT 6
- Enum null values should be null primitive instead of string HOT 1
- StringIndexOutOfBoundsException on SomeObject#get() HOT 3
- Could you please change the minimum data range so that could be less than -1 HOT 2
- Custom Attribute (**Implementation available**) HOT 5
- Support of @JsonProperty
- Support of oneOf
- Support for java.time.LocalTime HOT 2
- Create schema honouring @JsonProperty of a Pojo HOT 2
- The use of java.time.OffsetDateTime will cause StackOverflowError HOT 1
- Question: object mapper with custom configuration for schema generation HOT 10
- Removing guava dependency HOT 2
- Making dep javax.ws.rs-api optional HOT 1
- read from another project or desktop HOT 1
- Enum
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jjschema.