joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Many nested subclasses are genereted instead of one #775

Open sharifullin opened 7 years ago

sharifullin commented 7 years ago

See json schema below. Classes with underscore (after generating) are unwanted.

{
    "$schema": "http://json-schema.org/draft-04/schema#",

    "definitions": {

        "date": {"type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"},

        "parsedAddressRF": {"type": "object", "properties": {
            "zipCode": {"type": "string"},
            "regionCode": {"type": "string"},
            "kladrCode": {"type": "string"} }
        },

        "foreignAddress": {"type": "object", "properties": {
            "countryName": {"type": "string"},
            "addressString": {"type": "string"} }
        },

        "legalAddress": {"type": "object", "properties": {
            "parsedAddressRF": {"type": "object", "$ref": "#/definitions/parsedAddressRF"},
            "date": {"$ref": "#/definitions/date"} }
        },

        "branches": {"type": "array", "items": {"type": "object", "properties": {
            "name": {"type": "string"},
            "parsedAddressRF": {"type": "object", "$ref": "#/definitions/parsedAddressRF"},
            "foreignAddress": {"type": "object", "$ref": "#/definitions/foreignAddress"},
            "date": {"$ref": "#/definitions/date"} } }
        }

    },

    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "UL" : {"type": "object", "properties": {
                "legalAddress": {"type": "object", "$ref": "#/definitions/legalAddress" },
                "branches": {"type": "array", "$ref": "#/definitions/branches" },
                "history": {"type": "object", "properties":{
                    "legalAddresses" : {"type": "array", "items": {"$ref": "#/definitions/legalAddress"} },
                    "branches": {"type": "array", "$ref": "#/definitions/branches" }
                }}
            }
            }
        }
    }
}
joelittlejohn commented 7 years ago

Hi, what classes were generated and what did you want instead?

sharifullin commented 7 years ago

Following classes were generated Branch, Branch, ForeignAddress, ForeignAddress,LegalAddress, LegalAddress, ParsedAddressRF, ParsedAddressRF, ParsedAddressRF__, ParsedAddressRF__. But actually I don't need classes with an underscore symbol "".

mdfaizanali786 commented 7 years ago

I faced similar issue and then solved it by modifying the object rule and rule factory a bit. I created a Hash of Class Name, JSON node and the package using MD5 algorithm and stored it in a map. Before creating any new class I just check if it's hash already exists in the hashMap in which case I just return the class corresponding to that hash. Check my commit here : https://github.com/mdfaizanali786/jsonschema2pojo/commit/e5343db662ea648c5b0e559b3021aec43efd0299#diff-7fef584364765f2bd47a596ad18cecc4