1

Type.json (Enum)

{
    "type" : "string",
    "$schema" : "http://json-schema.org/draft-04/schema#",
    "title": "type resource",
    "name": "type resource",
    "description": "List of types",
    "id" : "type:v1",
    "enum" : 
        [
        "ACC1",
        "ACC2"
        ]
}

Pojo1.json

"properties":{
            "type" : {
                "$ref" : "Type.json"
            },
}

Pojo2.json

"properties":{
            "type" : {
                "$ref" : "Type.json"
            },
}

Instead of creating a separate java file for enum, it creates a enum inside one of the POJOs and this inner public enum is referred by the other POJO.

Pojo2.java

private com.Pojo1.Type type;

How to create a separate java file for enum ? Thanks

user104309
  • 690
  • 9
  • 20

3 Answers3

3

It doesn't look like creating enums as separate classes is supported in jsonschema2pojo as an option.

The generation of enums is carried out by org.jsonschema2pojo.rules.EnumRule

The javadoc of which states:

A Java {@link Enum} is created, with constants for each of the enum values present in the schema. The enum name is derived from the nodeName, and the enum type itself is created as an inner class of the owning type. In the rare case that no owning type exists (the enum is the root of the schema), then the enum becomes a public class in its own right.

Sounds like you'll need to raise a feature request

beresfordt
  • 5,088
  • 10
  • 35
  • 43
3

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.

s.muellner
  • 61
  • 4
0

This is possible I did it in this way:

 "atributes": {
  "description": "Additional message attributes",
  "type": "object",
  "properties": {
    "action": {
      "description": "Action to do over document",
      "$ref" : "Action.json#"
    }
  },
  "additionalProperties": false,
  "examples": "{\"action\":\"UPSERT\"}"
}

and action enum:

{

      "title": "Action",
      "description": "Action description",
      "type": "string",
      "enum": [
        "BATCH",
        "DELETE",
        "UNSUPPORTED"
      ],
      "javaEnumNames": [
        "UPSERT",
        "DELETE",
        "UNSUPPORTED"
      ]
    }