At the time of writing the post there was already a feature available in the jsonschema2pojo library. During my investigations I came over this post and was not pleased with the answer that it is not possible to reference a generated class or enum. So I digged a little deeper in the documentation, which did give me some additional extension properties to the usual json schema specification (javaTypes, javaInterfaces, javaEnumNames, javaJsonView, javaName). The documentation for the extension property javaType states:
jsonschema2pojo supports an extension property 'javaType' that applies to schemas and allows you to specify a fully qualified name for your generated Java type.
Given this knowledge your example needs to be rewritten like that:
Type.json (Enum)
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "string",
"title": "type resource",
"name": "type resource",
"description": "List of types",
"id" : "type:v1",
"type_enum": {
"javaType" : "com.mycompany.TypeEnum",
"$ref": "TypeEnum.json#/definitions/type_enum"
}
}
We now replace your enum definition by a reference $ref including a dedicated package path and class name for the javaType.
The referenced json schema for the according enum in your type class would then just look like that:
TypeEnum.json (Enum)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"type_enum": {
"enum": [
"ACC1",
"ACC2"
],
"description": "All values for the enum in the type class."
}
}
}
The Pojo definintions Pojo1 and Pojo2 are left untouched. You can even reference the enum in other classes like so:
TypeV1_1.json (Enum)
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "string",
"title": "type version 1.1 resource",
"name": "type version 1.1 resource",
"description": "List of version 1.1 types",
"id" : "type:v1_1",
"type_enum": {
"javaType" : "com.mycompany.TypeEnum",
"$ref": "TypeEnum.json#/definitions/type_enum"
}
}
leaving you always with only one generated enum for your defined javaType.