how to generate classes out of the json schema

1.7k views Asked by At

I have a json schema from which I need to generate java classes

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://schema.softsense.io/analysis/vulnerabilities/v1alpha1/vulnerabilities.json",
    "title": "Vulnerabilities",
    "description": "SoftSense Vulnerabilities analysis schema v1alpha3",
    
    "type": "object",
    "allOf": [ { "$ref": "https://schema.softsense.io/analysis/v1alpha3/base.json" } ],
    "properties": {
        "vulnerabilities": {
            "items": {
                "$ref": "#/definitions/Vulnerability"
            },
            "type": "array"
        }
    },
    "oneOf": [{ "required": ["vulnerabilities"] }, { "required": ["error"] }],
    "definitions": {
        "Vulnerability": {
            "required": [
                "id",
                "description",
                "sources"
            ],
            "properties": {
                "id": {
                    "description": "Identifier for the vulnerability, such as a CVE identifier (CVE-2021-12345).",
                    "type": "string"
                },
                "published": {
                    "description": "UTC time when the vulnerability was published",
                    "type": "string",
                    "format": "date-time"
                },
                "updated": {
                    "description": "UTC time when the vulnerability was updated",
                    "type": "string",
                    "format": "date-time"
                },
                "description": {
                    "description": "Description of the vulnerability",
                    "type": "string"
                },
                "sources": {
                    "description": "The collection of backing vulnerability sources that listed this vulnerability for the software component",
                    "items": {
                        "type": "string"
                    },
                    "type": "array"
                },
                "cwes": {
                    "description": "The collection of Common Weakness Enumeration (CWE) identifiers that characterize the vulnerability.",
                    "items": {
                        "$ref": "#/definitions/CWE"
                    },
                    "type": "array"
                },
                "references": {
                    "description": "External URLs related to the vulnerability, such as issue trackers, mailing list discussions, commits, or patches.",
                    "items": {
                        "$ref": "#/definitions/Reference"
                    },
                    "type": "array"
                },
                "impact": {
                    "description": "What impact this vulnerability has",
                    "$ref": "#/definitions/Impact"
                }
            },
            "type": "object"
        },
        "Impact": {
            "properties": {
                "metricV3": {
                    "$ref": "#/definitions/ImpactMetricV3"
                },
                "metricV2": {
                    "$ref": "#/definitions/ImpactMetricV2"
                },
                "*": {
                    "type": "object"
                }
            },
            "type": "object"
        },
        "ImpactMetricV3": {
            "properties": {
                "cvssV3": {
                    "$ref": "#/definitions/CVSSv3"
                },
                "exploitabilityScore": {
                    "type": "number"
                },
                "impactScore": {
                    "type": "number"
                }
            }
        },
        "CVSSv3": {
            "required": [
                "version",
                "vectorString",
                "baseScore",
                "baseSeverity"
            ],
            "properties": {
                "version": {
                    "type": "string"
                },
                "vectorString": {
                    "type": "string"
                },
                "attackVector": {
                    "type": "string"
                },
                "attackComplexity": {
                    "type": "string"
                },
                "privilegesRequired": {
                    "type": "string"
                },
                "userInteraction": {
                    "type": "string"
                },
                "scope": {
                    "type": "string"
                },
                "confidentialityImpact": {
                    "type": "string"
                },
                "integrityImpact": {
                    "type": "string"
                },
                "availabilityImpact": {
                    "type": "string"
                },
                "baseScore": {
                    "type": "number"
                },
                "baseSeverity": {
                    "type": "string"
                },
                "exploitCodeMaturity": {
                    "type": "string"
                },
                "remediationLevel": {
                    "type": "string"
                },
                "reportConfidence": {
                    "type": "string"
                },
                "temporalScore": {
                    "type": "number"
                },
                "temporalSeverity": {
                    "type": "string"
                },
                "confidentialityRequirement": {
                    "type": "string"
                },
                "integrityRequirement": {
                    "type": "string"
                },
                "availabilityRequirement": {
                    "type": "string"
                },
                "modifiedAttackVector": {
                    "type": "string"
                },
                "modifiedAttackComplexity": {
                    "type": "string"
                },
                "modifiedPrivilegesRequired": {
                    "type": "string"
                },
                "modifiedUserInteraction": {
                    "type": "string"
                },
                "modifiedScope": {
                    "type": "string"
                },
                "modifiedConfidentialityImpact": {
                    "type": "string"
                },
                "modifiedIntegrityImpact": {
                    "type": "string"
                },
                "modifiedAvailabilityImpact": {
                    "type": "string"
                },
                "environmentalScore": {
                    "type": "number"
                },
                "environmentalSeverity": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        "ImpactMetricV2": {
            "required": [
                "cvssV2",
                "severity",
                "exploitabilityScore",
                "impactScore",
                "obtainAllPrivilege",
                "obtainUserPrivilege",
                "obtainOtherPrivilege",
                "userInteractionRequired"
            ],
            "properties": {
                "cvssV2": {
                "$ref": "#/definitions/CVSSv2"
                },
                "severity": {
                "type": "string"
                },
                "exploitabilityScore": {
                "type": "number"
                },
                "impactScore": {
                "type": "number"
                },
                "acInsufInfo": {
                "type": "boolean"
                },
                "obtainAllPrivilege": {
                "type": "boolean"
                },
                "obtainUserPrivilege": {
                "type": "boolean"
                },
                "obtainOtherPrivilege": {
                "type": "boolean"
                },
                "userInteractionRequired": {
                "type": "boolean"
                }
            },
            "type": "object"
        },
        "CVSSv2": {
            "required": [
                "version",
                "vectorString",
                "baseScore"
            ],
            "properties": {
                "version": {
                    "type": "string"
                },
                "vectorString": {
                    "type": "string"
                },
                "accessVector": {
                    "type": "string"
                },
                "accessComplexity": {
                    "type": "string"
                },
                "authentication": {
                    "type": "string"
                },
                "confidentialityImpact": {
                    "type": "string"
                },
                "integrityImpact": {
                    "type": "string"
                },
                "availabilityImpact": {
                    "type": "string"
                },
                "baseScore": {
                    "type": "number"
                },
                "exploitability": {
                    "type": "string"
                },
                "remediationLevel": {
                    "type": "string"
                },
                "reportConfidence": {
                    "type": "string"
                },
                "temporalScore": {
                    "type": "number"
                },
                "collateralDamagePotential": {
                    "type": "string"
                },
                "targetDistribution": {
                    "type": "string"
                },
                "confidentialityRequirement": {
                    "type": "string"
                },
                "integrityRequirement": {
                    "type": "string"
                },
                "availabilityRequirement": {
                    "type": "string"
                },
                "environmentalScore": {
                    "type": "number"
                }
            },
            "type": "object"
        },
        "CWE": {
            "required": [
                "name"
            ],
            "properties": {
                "name": {
                    "type": "string"
                },
                "description": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        "Reference": {
            "required": [
                "url"
            ],
            "properties": {
                "name": {
                    "type": "string"
                },
                "url": {
                    "type": "string"
                },
                "tags": {
                    "items": {
                        "type": "string"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        }
    }
}

I am trying to use the jsonschema2pojo as under.

   <plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>0.4.34</version>
                <configuration>
                    <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
                    <targetPackage>com.test.gen</targetPackage>
                    <useCommonsLang3>true</useCommonsLang3>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

For some reason I get the following error when i use mvn generate-sources

[ERROR] Failed to execute goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate (default) on project hsk: Execution default of goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate failed: String index out of range: 0 -> [Help 1]

Any help is highly appreciated

Regards,

1

There are 1 answers

0
DwB On

Remove the following section from the "Impact" definition:

"*": {
    "type": "object"
}

It appears to be an invalid definition.