Repository: victools/jsonschema-generator
Pull request #74 (2020-04-12 20:12:05)

Add official support for Draft 6
The differences between Draft 6 and 7 are not that big – might as well also support Draft 6 then.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5e8aa15..8ebc13a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ## [Unreleased]
 ### `jsonschema-generator`
 #### Added
+- Official support for Draft 6 (via new `SchemaVersion.DRAFT_6`)
 - New `Option.INLINE_ALL_SCHEMAS` to enforce no `definitions`/`$defs` to be produced (throwing exception if there is at least one circular reference)
 - Offering also `SchemaGenerationContext.createStandardDefinition(FieldScope, CustomPropertyDefinitionProvider)`
 - Offering also `SchemaGenerationContext.createStandardDefinition(MethodScope, CustomPropertyDefinitionProvider)`
diff --git a/README.md b/README.md
index 1cd1fb0..51b068b 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
 [![Build Status](https://github.com/victools/jsonschema-generator/workflows/Java%20CI%20(Maven)/badge.svg)](https://github.com/victools/jsonschema-generator/actions?query=workflow%3A%22Java+CI+%28Maven%29%22)
 [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.victools/jsonschema-generator/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.victools/jsonschema-generator)
 
-Creating JSON Schema (Draft 7 or Draft 2019-09) from your Java classes utilising Jackson.
+Creating JSON Schema (Draft 6, Draft 7 or Draft 2019-09) from your Java classes utilising Jackson.
 
 ----
 
diff --git a/jsonschema-generator/README.md b/jsonschema-generator/README.md
index 428572a..626cb91 100644
--- a/jsonschema-generator/README.md
+++ b/jsonschema-generator/README.md
@@ -1,7 +1,7 @@
 # Java JSON Schema Generator
 [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.victools/jsonschema-generator/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.victools/jsonschema-generator)
 
-Creating JSON Schema (Draft 7 or Draft 2019-09) from your Java classes utilising Jackson (inspired by JJSchema).
+Creating JSON Schema (Draft 6, Draft 7 or Draft 2019-09) from your Java classes utilising Jackson (inspired by JJSchema).
 
 Topics covered in this document are:
 - [Usage](#usage)
diff --git a/jsonschema-generator/pom.xml b/jsonschema-generator/pom.xml
index ac9948a..b1d6c66 100644
--- a/jsonschema-generator/pom.xml
+++ b/jsonschema-generator/pom.xml
@@ -10,7 +10,7 @@
     <artifactId>jsonschema-generator</artifactId>
     
     <name>Java JSON Schema Generator</name>
-    <description>Java JSON Schema Generator – creating a JSON Schema (Draft 7 or Draft 2019-09) from your Java classes</description>
+    <description>Java JSON Schema Generator – creating a JSON Schema (Draft 6, Draft 7 or Draft 2019-09) from your Java classes</description>
     <url>https://github.com/victools/jsonschema-generator</url>
 
     <dependencies>
diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java
index d6ca642..588a5de 100644
--- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java
+++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java
@@ -320,7 +320,7 @@ public class SchemaGenerator {
         Map<String, List<JsonNode>> fieldsFromAllParts = Stream.concat(Stream.of(schemaNode), parts.stream())
                 .flatMap(part -> StreamSupport.stream(((Iterable<Map.Entry<String, JsonNode>>) () -> part.fields()).spliterator(), false))
                 .collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
-        if (this.config.getSchemaVersion() == SchemaVersion.DRAFT_7
+        if ((this.config.getSchemaVersion() == SchemaVersion.DRAFT_6 || this.config.getSchemaVersion() == SchemaVersion.DRAFT_7)
                 && fieldsFromAllParts.containsKey(this.config.getKeyword(SchemaKeyword.TAG_REF))) {
             // in Draft 7, any other attributes besides the $ref keyword were ignored
             return;
diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaKeyword.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaKeyword.java
index d2950df..5f44457 100644
--- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaKeyword.java
+++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaKeyword.java
@@ -29,10 +29,10 @@ public enum SchemaKeyword {
      * Beware that this keyword was only introduced in {@link SchemaVersion#DRAFT_2019_09}.
      */
     TAG_ANCHOR("$anchor"),
-    TAG_DEFINITIONS(version -> version == SchemaVersion.DRAFT_7 ? "definitions" : "$defs"),
+    TAG_DEFINITIONS(version -> version == SchemaVersion.DRAFT_6 || version == SchemaVersion.DRAFT_7 ? "definitions" : "$defs"),
     TAG_REF("$ref"),
     TAG_REF_MAIN("#"),
-    TAG_REF_PREFIX(version -> version == SchemaVersion.DRAFT_7 ? "#/definitions/" : "#/$defs/"),
+    TAG_REF_PREFIX(version -> version == SchemaVersion.DRAFT_6 || version == SchemaVersion.DRAFT_7 ? "#/definitions/" : "#/$defs/"),
 
     TAG_TYPE("type"),
     TAG_TYPE_NULL("null"),
@@ -74,8 +74,17 @@ public enum SchemaKeyword {
     TAG_ITEMS_MAX("maxItems"),
     TAG_ITEMS_UNIQUE("uniqueItems"),
 
+    /**
+     * Beware that this keyword was only introduced in {@link SchemaVersion#DRAFT_7}.
+     */
     TAG_IF("if"),
+    /**
+     * Beware that this keyword was only introduced in {@link SchemaVersion#DRAFT_7}.
+     */
     TAG_THEN("then"),
+    /**
+     * Beware that this keyword was only introduced in {@link SchemaVersion#DRAFT_7}.
+     */
     TAG_ELSE("else");
 
     private final Function<SchemaVersion, String> valueProvider;
diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaVersion.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaVersion.java
index 6ee91e5..480dae6 100644
--- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaVersion.java
+++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaVersion.java
@@ -20,6 +20,7 @@ package com.github.victools.jsonschema.generator;
  * Supported JSON Schema Versions.
  */
 public enum SchemaVersion {
+    DRAFT_6("http://json-schema.org/draft-06/schema#"),
     DRAFT_7("http://json-schema.org/draft-07/schema#"),
     DRAFT_2019_09("https://json-schema.org/draft/2019-09/schema");
 
diff --git a/jsonschema-module-javax-validation/src/main/java/com/github/victools/jsonschema/module/javax/validation/JavaxValidationOption.java b/jsonschema-module-javax-validation/src/main/java/com/github/victools/jsonschema/module/javax/validation/JavaxValidationOption.java
index 051c8d1..5370cf7 100644
--- a/jsonschema-module-javax-validation/src/main/java/com/github/victools/jsonschema/module/javax/validation/JavaxValidationOption.java
+++ b/jsonschema-module-javax-validation/src/main/java/com/github/victools/jsonschema/module/javax/validation/JavaxValidationOption.java
@@ -30,6 +30,8 @@ public enum JavaxValidationOption {
     NOT_NULLABLE_METHOD_IS_REQUIRED,
     /**
      * Use this option to indicate the "idn-email" format instead of "email" when an {@code @Email} annotation is being found.
+     * <br>
+     * Beware that this format was only introduced in {@code SchemaVersion.DRAFT_7}.
      */
     PREFER_IDN_EMAIL_FORMAT,
     /**
diff --git a/pom.xml b/pom.xml
index 5b9866a..bc7e950 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
 	<modelVersion>4.0.0</modelVersion>
     
     <name>Java JSON Schema Generator (Parent)</name>
-    <description>Java JSON Schema Generator + Modules – creating a JSON Schema (Draft 7 or Draft 2019-09) from your Java classes</description>
+    <description>Java JSON Schema Generator + Modules – creating a JSON Schema (Draft 6, Draft 7 or Draft 2019-09) from your Java classes</description>
     <url>https://github.com/victools/jsonschema-generator</url>
 
     <groupId>com.github.victools</groupId>


Test patch:
diff --git a/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorAllOfCleanUpTest.java b/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorAllOfCleanUpTest.java
index 4fddd6e..9b952f7 100644
--- a/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorAllOfCleanUpTest.java
+++ b/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorAllOfCleanUpTest.java
@@ -60,10 +60,12 @@ public class SchemaGeneratorAllOfCleanUpTest {
                 .flatMap(oneVersion -> Arrays.asList(oneVersion).stream())
                 .collect(Collectors.toList());
 
-        // in Draft 7, alongside $ref all other attributes are being ignored and should therefore not be merged
+        // in Drafts 6/7, alongside $ref all other attributes are being ignored and should therefore not be merged
+        EnumSet<SchemaVersion> versionsWithSpecialRefHandling = EnumSet.of(SchemaVersion.DRAFT_6, SchemaVersion.DRAFT_7);
         String refInAllOfPart = "{ \"type\": \"object\", \"allOf\": [{ \"$ref\": \"#\" }, { \"title\": \"value\" }] }";
-        testCases.add(new Object[]{ SchemaVersion.DRAFT_7, refInAllOfPart, refInAllOfPart });
-        EnumSet.complementOf(EnumSet.of(SchemaVersion.DRAFT_7)).stream()
+        versionsWithSpecialRefHandling.stream()
+                .forEach(schemaVersion -> testCases.add(new Object[]{ schemaVersion, refInAllOfPart, refInAllOfPart }));
+        EnumSet.complementOf(versionsWithSpecialRefHandling).stream()
                 .forEach(schemaVersion ->
         testCases.add(new Object[]{ schemaVersion, refInAllOfPart, "{ \"type\": \"object\", \"$ref\": \"#\", \"title\": \"value\" }" }));
         return testCases.toArray();
diff --git a/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/circular-custom-definition-DRAFT_6.json b/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/circular-custom-definition-DRAFT_6.json
new file mode 100644
index 0000000..a76e9cd
--- /dev/null
+++ b/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/circular-custom-definition-DRAFT_6.json
@@ -0,0 +1,34 @@
+{
+    "definitions": {
+        "List(TestCircularClass1)-nullable": {
+            "type": ["object", "null"],
+            "properties": {
+                "get(0)": {
+                    "$ref": "#"
+                }
+            }
+        },
+        "List(TestCircularClass2)-nullable": {
+            "type": ["object", "null"],
+            "properties": {
+                "get(0)": {
+                    "$ref": "#/definitions/TestCircularClass2"
+                }
+            }
+        },
+        "TestCircularClass2": {
+            "type": "object",
+            "properties": {
+                "list1": {
+                    "$ref": "#/definitions/List(TestCircularClass1)-nullable"
+                }
+            }
+        }
+    },
+    "type": "object",
+    "properties": {
+        "list2": {
+            "$ref": "#/definitions/List(TestCircularClass2)-nullable"
+        }
+    }
+}
diff --git a/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/custom-property-definition-DRAFT_6.json b/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/custom-property-definition-DRAFT_6.json
new file mode 100644
index 0000000..cbc8af8
--- /dev/null
+++ b/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/custom-property-definition-DRAFT_6.json
@@ -0,0 +1,24 @@
+{
+    "$schema": "http://json-schema.org/draft-06/schema#",
+    "type": "object",
+    "properties": {
+        "number": {
+            "type": "integer",
+            "title": "custom title",
+            "description": "field description"
+        },
+        "self": {
+            "allOf": [{
+                    "$ref": "#"
+                }, {
+                    "description": "field description"
+                }]
+        },
+        "text": {
+            "type": "string",
+            "title": "type title",
+            "description": "custom description"
+        }
+    },
+    "title": "type title"
+}
diff --git a/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/multiple-definitions-one-type-DRAFT_6.json b/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/multiple-definitions-one-type-DRAFT_6.json
new file mode 100644
index 0000000..5defbf7
--- /dev/null
+++ b/jsonschema-generator/src/test/resources/com/github/victools/jsonschema/generator/multiple-definitions-one-type-DRAFT_6.json
@@ -0,0 +1,57 @@
+{
+    "$schema": "http://json-schema.org/draft-06/schema#",
+    "definitions": {
+        "String": {
+            "title": "Custom Definition #1 for String",
+            "anyOf": [{
+                    "type": "string"
+                }, {
+                    "type": "null"
+                }]
+        },
+        "TestDirectCircularClass-1": {
+            "type": "object",
+            "properties": {
+                "number": {
+                    "$ref": "#/definitions/int-2"
+                },
+                "self": {
+                    "$ref": "#"
+                },
+                "text": {
+                    "$ref": "#/definitions/String"
+                }
+            }
+        },
+        "TestDirectCircularClass-2": {
+            "title": "Custom Definition #2 for com.github.victools.jsonschema.generator.SchemaGeneratorCustomDefinitionsTest$TestDirectCircularClass",
+            "anyOf": [{
+                    "$ref": "#/definitions/TestDirectCircularClass-1"
+                }, {
+                    "type": "null"
+                }]
+        },
+        "int-1": {
+            "title": "Custom Definition #2 for int",
+            "anyOf": [{
+                    "type": "integer"
+                }, {
+                    "type": "null"
+                }]
+        },
+        "int-2": {
+            "title": "Custom Definition #1 for int",
+            "anyOf": [{
+                    "$ref": "#/definitions/int-1"
+                }, {
+                    "type": "null"
+                }]
+        }
+    },
+    "title": "Custom Definition #1 for TestDirectCircularClass",
+    "anyOf": [{
+            "$ref": "#/definitions/TestDirectCircularClass-2"
+        }, {
+            "type": "null"
+        }]
+}