Jackson: How can I generate json schema which rejects all additional content












11















I want to generate JSON schema where "additionalProperties" : false will be applied for all classes which I have.



Suppose I have following classes:



class A{
private String s;
private B b;

public String getS() {
return s;
}

public B getB() {
return b;
}
}

class B{
private BigDecimal bd;

public BigDecimal getBd() {
return bd;
}
}


When I am generating schema as following like below code the schema property "additionalProperties" : false was applying only for the class A.



ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
schema.rejectAdditionalProperties();
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);


How can I generate the schema where "additionalProperties" : false will be applied on all classes?



Example of schema



{
"type" : "object",
"id" : "urn:jsonschema:com.xxx.xxx:A",
"additionalProperties" : false,
"properties" : {
"s" : {
"type" : "string"
},
"b" : {
"type" : "object",
"id" : "urn:jsonschema:com.xxx.xxx:B",
"properties" : {
"bd" : {
"type" : "number"
}
}
}
}
}



Note: I don't want to generate schemes part by part.



For info:
I have opened issue for this scenario if someone interested you can support fix of this issue. Generate json schema which should rejects all additional content










share|improve this question





























    11















    I want to generate JSON schema where "additionalProperties" : false will be applied for all classes which I have.



    Suppose I have following classes:



    class A{
    private String s;
    private B b;

    public String getS() {
    return s;
    }

    public B getB() {
    return b;
    }
    }

    class B{
    private BigDecimal bd;

    public BigDecimal getBd() {
    return bd;
    }
    }


    When I am generating schema as following like below code the schema property "additionalProperties" : false was applying only for the class A.



    ObjectMapper mapper = new ObjectMapper();
    JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
    ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
    schema.rejectAdditionalProperties();
    mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);


    How can I generate the schema where "additionalProperties" : false will be applied on all classes?



    Example of schema



    {
    "type" : "object",
    "id" : "urn:jsonschema:com.xxx.xxx:A",
    "additionalProperties" : false,
    "properties" : {
    "s" : {
    "type" : "string"
    },
    "b" : {
    "type" : "object",
    "id" : "urn:jsonschema:com.xxx.xxx:B",
    "properties" : {
    "bd" : {
    "type" : "number"
    }
    }
    }
    }
    }



    Note: I don't want to generate schemes part by part.



    For info:
    I have opened issue for this scenario if someone interested you can support fix of this issue. Generate json schema which should rejects all additional content










    share|improve this question



























      11












      11








      11








      I want to generate JSON schema where "additionalProperties" : false will be applied for all classes which I have.



      Suppose I have following classes:



      class A{
      private String s;
      private B b;

      public String getS() {
      return s;
      }

      public B getB() {
      return b;
      }
      }

      class B{
      private BigDecimal bd;

      public BigDecimal getBd() {
      return bd;
      }
      }


      When I am generating schema as following like below code the schema property "additionalProperties" : false was applying only for the class A.



      ObjectMapper mapper = new ObjectMapper();
      JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
      ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
      schema.rejectAdditionalProperties();
      mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);


      How can I generate the schema where "additionalProperties" : false will be applied on all classes?



      Example of schema



      {
      "type" : "object",
      "id" : "urn:jsonschema:com.xxx.xxx:A",
      "additionalProperties" : false,
      "properties" : {
      "s" : {
      "type" : "string"
      },
      "b" : {
      "type" : "object",
      "id" : "urn:jsonschema:com.xxx.xxx:B",
      "properties" : {
      "bd" : {
      "type" : "number"
      }
      }
      }
      }
      }



      Note: I don't want to generate schemes part by part.



      For info:
      I have opened issue for this scenario if someone interested you can support fix of this issue. Generate json schema which should rejects all additional content










      share|improve this question
















      I want to generate JSON schema where "additionalProperties" : false will be applied for all classes which I have.



      Suppose I have following classes:



      class A{
      private String s;
      private B b;

      public String getS() {
      return s;
      }

      public B getB() {
      return b;
      }
      }

      class B{
      private BigDecimal bd;

      public BigDecimal getBd() {
      return bd;
      }
      }


      When I am generating schema as following like below code the schema property "additionalProperties" : false was applying only for the class A.



      ObjectMapper mapper = new ObjectMapper();
      JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
      ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
      schema.rejectAdditionalProperties();
      mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);


      How can I generate the schema where "additionalProperties" : false will be applied on all classes?



      Example of schema



      {
      "type" : "object",
      "id" : "urn:jsonschema:com.xxx.xxx:A",
      "additionalProperties" : false,
      "properties" : {
      "s" : {
      "type" : "string"
      },
      "b" : {
      "type" : "object",
      "id" : "urn:jsonschema:com.xxx.xxx:B",
      "properties" : {
      "bd" : {
      "type" : "number"
      }
      }
      }
      }
      }



      Note: I don't want to generate schemes part by part.



      For info:
      I have opened issue for this scenario if someone interested you can support fix of this issue. Generate json schema which should rejects all additional content







      java json jackson jsonschema json-schema-validator






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 8:14







      Beno Arakelyan

















      asked Feb 26 '18 at 8:30









      Beno ArakelyanBeno Arakelyan

      553416




      553416
























          4 Answers
          4






          active

          oldest

          votes


















          3





          +25









          You will need to specify the schema for each properties like:



          ObjectMapper mapper = new ObjectMapper();
          JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
          ObjectSchema schemaB = schemaGen.generateSchema(B.class).asObjectSchema();
          schemaB.rejectAdditionalProperties();

          ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
          schema.rejectAdditionalProperties();
          schema.putProperty("b", schemaB);


          You can leverage reflection api to automatically do it for you. Here is a quick and dirty example:



          public static void main(String args) throws JsonProcessingException {
          final ObjectMapper mapper = new ObjectMapper();
          final JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
          ObjectSchema schema = generateSchema(schemaGen, A.class);
          schema.rejectAdditionalProperties();
          System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
          }

          public static <T> ObjectSchema generateSchema(JsonSchemaGenerator generator, Class<T> type) throws JsonMappingException {
          ObjectSchema schema = generator.generateSchema(type).asObjectSchema();
          for (final Field field : type.getDeclaredFields()) {
          if (!field.getType().getName().startsWith("java") && !field.getType().isPrimitive()) {
          final ObjectSchema fieldSchema = generateSchema(generator, field.getType());
          fieldSchema.rejectAdditionalProperties();
          schema.putProperty(field.getName(), fieldSchema);
          }
          }
          return schema;
          }





          share|improve this answer


























          • Thanks for answer but I don't want to put properties manually. Does Jackson have any api to provide that?

            – Beno Arakelyan
            Feb 28 '18 at 10:47











          • In my answer i suggested to use java reflection API to do it automatically. I don't know any other way to do it.

            – JEY
            Feb 28 '18 at 10:50











          • I updated my answer with quick reflection usage.

            – JEY
            Feb 28 '18 at 11:05



















          1














          The following worked for me:



          import com.fasterxml.jackson.databind.JsonNode;
          import com.fasterxml.jackson.databind.ObjectMapper;
          import com.kjetland.jackson.jsonSchema.JsonSchemaConfig;
          import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;

          ...

          ObjectMapper objectMapper = new ObjectMapper();
          JsonSchemaConfig config = JsonSchemaConfig.nullableJsonSchemaDraft4();
          JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper, config);
          JsonNode jsonNode = schemaGenerator.generateJsonSchema(Test.class);
          String jsonSchemaText = jsonNode.toString();


          Using maven dependency:



          <dependency>
          <groupId>com.kjetland</groupId>
          <artifactId>mbknor-jackson-jsonschema_2.12</artifactId>
          <version>1.0.28</version>
          </dependency>


          Using the following classes:



          Test.java:



          import com.fasterxml.jackson.annotation.JsonCreator;
          import com.fasterxml.jackson.annotation.JsonProperty;

          public class Test {
          @JsonProperty(required = true)
          private final String name;
          private final TestChild child;

          @JsonCreator
          public Test (
          @JsonProperty("name") String name,
          @JsonProperty("child") TestChild child) {
          this.name = name;
          this.child = child;
          }

          public String getName () {
          return name;
          }

          public TestChild getChild () {
          return child;
          }
          }


          ...and TestChild.java:



          import com.fasterxml.jackson.annotation.JsonCreator;
          import com.fasterxml.jackson.annotation.JsonProperty;

          public class TestChild {
          @JsonProperty(required = true)
          private final String childName;

          @JsonCreator
          public TestChild (@JsonProperty("childName") String childName) {
          this.childName = childName;
          }

          public String getChildName () {
          return childName;
          }
          }


          Results in (output of jsonSchemaText piped through jq -C . for pretty formatting):



          {
          "$schema": "http://json-schema.org/draft-04/schema#",
          "title": "Test",
          "type": "object",
          "additionalProperties": false,
          "properties": {
          "name": {
          "type": "string"
          },
          "child": {
          "oneOf": [
          {
          "type": "null",
          "title": "Not included"
          },
          {
          "$ref": "#/definitions/TestChild"
          }
          ]
          }
          },
          "required": [
          "name"
          ],
          "definitions": {
          "TestChild": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
          "childName": {
          "type": "string"
          }
          },
          "required": [
          "childName"
          ]
          }
          }
          }


          This results in "additionalProperties": false on both Test and TestChild.



          Note: You can replace JsonSchemaConfig.nullableJsonSchemaDraft4() with JsonSchemaConfig.vanillaJsonSchemaDraft4() in your schema generation code to get rid of the "oneof" references with "type: null" or "type: ActualType" in favor of just "type: ActualType" (note, this still won't add them to the "required" array unless you annotate the properties with @JsonProperty(required = true)).






          share|improve this answer


























          • Notice this also results in "reference" based schemas, as opposed to flattened schemas (which won't allow self-referencing structures such as Trees where a Branch may contain Branches and/or Leaves) such as shown in the example provided in the question.

            – Shadow Man
            Mar 6 '18 at 2:30













          • Thanks. I will try your solution and let you know.

            – Beno Arakelyan
            Mar 7 '18 at 9:32











          • It is the scala library. Are there the java library which provides same functionality ?

            – Beno Arakelyan
            Mar 7 '18 at 23:37











          • I'm using this and I'm not using scala at all. I'm using pure java. The library may have been written in scala, but it certainly works in java. Personally, I don't see this as something to use in a service somewhere. I see this as something to run in a maven plugin. We wrote such a plugin at my work to generate the schema from our model classes as part of the maven build process and upload it to an S3 bucket, so we don't even pull in those libs to our generated jars.

            – Shadow Man
            Mar 8 '18 at 1:50





















          0














          Well I would go to a simpler route if you don't want to use reflections. I would use JSONPath. So you would need to add below to your pom.xml



            <dependency>
          <groupId>com.jayway.jsonpath</groupId>
          <artifactId>json-path</artifactId>
          <version>2.3.0</version>
          </dependency>


          Then below code demonstrates how to alter the generated JSON file



          package taruntest;

          import com.jayway.jsonpath.*;

          public class Test {

          public static void main(String args) throws Exception {
          String data = "{n" +
          " "type" : "object",n" +
          " "id" : "urn:jsonschema:com.xxx.xxx:A",n" +
          " "additionalProperties" : false,n" +
          " "properties" : {n" +
          " "s" : {n" +
          " "type" : "string"n" +
          " },n" +
          " "b" : {n" +
          " "type" : "object",n" +
          " "id" : "urn:jsonschema:com.xxx.xxx:B",n" +
          " "properties" : {n" +
          " "bd" : {n" +
          " "type" : "number"n" +
          " }n" +
          " }n" +
          " }n" +
          " }n" +
          "}";

          DocumentContext doc = JsonPath.parse(data);
          doc.put("$..[?(@.id =~ /urn:jsonschema:.*/)]", "additionalProperties", false);
          String modified = doc.jsonString();
          System.out.println(modified);
          }
          }


          The output of the run is (formatted manually)



          {
          "type": "object",
          "id": "urn:jsonschema:com.xxx.xxx:A",
          "additionalProperties": false,
          "properties": {
          "s": {
          "type": "string"
          },
          "b": {
          "type": "object",
          "id": "urn:jsonschema:com.xxx.xxx:B",
          "properties": {
          "bd": {
          "type": "number"
          }
          },
          "additionalProperties": false
          }
          }
          }





          share|improve this answer































            -2














            You can achieve this by impplementing Interface in those classes:



            public interface I {
            boolean additionalProperties = false;
            }

            public class A implements I {
            private String s;
            private B b;

            public String getS() {
            return s;
            }

            public B getB() {
            return b;
            }
            }

            public class B implements I {
            private BigDecimal bd;

            public BigDecimal getBd() {
            return bd;
            }
            }





            share|improve this answer



















            • 1





              Implement the interface which contains only properties is bad practice.

              – Beno Arakelyan
              Feb 28 '18 at 10:56













            • @BenoArakelyan and JsonSchemaGenerator doesn't read the value of the property.

              – JEY
              Feb 28 '18 at 10:57











            • sven.kwiotek It is mainly bad practice in java.

              – Beno Arakelyan
              Feb 28 '18 at 11:00











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48984259%2fjackson-how-can-i-generate-json-schema-which-rejects-all-additional-content%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3





            +25









            You will need to specify the schema for each properties like:



            ObjectMapper mapper = new ObjectMapper();
            JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
            ObjectSchema schemaB = schemaGen.generateSchema(B.class).asObjectSchema();
            schemaB.rejectAdditionalProperties();

            ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
            schema.rejectAdditionalProperties();
            schema.putProperty("b", schemaB);


            You can leverage reflection api to automatically do it for you. Here is a quick and dirty example:



            public static void main(String args) throws JsonProcessingException {
            final ObjectMapper mapper = new ObjectMapper();
            final JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
            ObjectSchema schema = generateSchema(schemaGen, A.class);
            schema.rejectAdditionalProperties();
            System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
            }

            public static <T> ObjectSchema generateSchema(JsonSchemaGenerator generator, Class<T> type) throws JsonMappingException {
            ObjectSchema schema = generator.generateSchema(type).asObjectSchema();
            for (final Field field : type.getDeclaredFields()) {
            if (!field.getType().getName().startsWith("java") && !field.getType().isPrimitive()) {
            final ObjectSchema fieldSchema = generateSchema(generator, field.getType());
            fieldSchema.rejectAdditionalProperties();
            schema.putProperty(field.getName(), fieldSchema);
            }
            }
            return schema;
            }





            share|improve this answer


























            • Thanks for answer but I don't want to put properties manually. Does Jackson have any api to provide that?

              – Beno Arakelyan
              Feb 28 '18 at 10:47











            • In my answer i suggested to use java reflection API to do it automatically. I don't know any other way to do it.

              – JEY
              Feb 28 '18 at 10:50











            • I updated my answer with quick reflection usage.

              – JEY
              Feb 28 '18 at 11:05
















            3





            +25









            You will need to specify the schema for each properties like:



            ObjectMapper mapper = new ObjectMapper();
            JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
            ObjectSchema schemaB = schemaGen.generateSchema(B.class).asObjectSchema();
            schemaB.rejectAdditionalProperties();

            ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
            schema.rejectAdditionalProperties();
            schema.putProperty("b", schemaB);


            You can leverage reflection api to automatically do it for you. Here is a quick and dirty example:



            public static void main(String args) throws JsonProcessingException {
            final ObjectMapper mapper = new ObjectMapper();
            final JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
            ObjectSchema schema = generateSchema(schemaGen, A.class);
            schema.rejectAdditionalProperties();
            System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
            }

            public static <T> ObjectSchema generateSchema(JsonSchemaGenerator generator, Class<T> type) throws JsonMappingException {
            ObjectSchema schema = generator.generateSchema(type).asObjectSchema();
            for (final Field field : type.getDeclaredFields()) {
            if (!field.getType().getName().startsWith("java") && !field.getType().isPrimitive()) {
            final ObjectSchema fieldSchema = generateSchema(generator, field.getType());
            fieldSchema.rejectAdditionalProperties();
            schema.putProperty(field.getName(), fieldSchema);
            }
            }
            return schema;
            }





            share|improve this answer


























            • Thanks for answer but I don't want to put properties manually. Does Jackson have any api to provide that?

              – Beno Arakelyan
              Feb 28 '18 at 10:47











            • In my answer i suggested to use java reflection API to do it automatically. I don't know any other way to do it.

              – JEY
              Feb 28 '18 at 10:50











            • I updated my answer with quick reflection usage.

              – JEY
              Feb 28 '18 at 11:05














            3





            +25







            3





            +25



            3




            +25





            You will need to specify the schema for each properties like:



            ObjectMapper mapper = new ObjectMapper();
            JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
            ObjectSchema schemaB = schemaGen.generateSchema(B.class).asObjectSchema();
            schemaB.rejectAdditionalProperties();

            ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
            schema.rejectAdditionalProperties();
            schema.putProperty("b", schemaB);


            You can leverage reflection api to automatically do it for you. Here is a quick and dirty example:



            public static void main(String args) throws JsonProcessingException {
            final ObjectMapper mapper = new ObjectMapper();
            final JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
            ObjectSchema schema = generateSchema(schemaGen, A.class);
            schema.rejectAdditionalProperties();
            System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
            }

            public static <T> ObjectSchema generateSchema(JsonSchemaGenerator generator, Class<T> type) throws JsonMappingException {
            ObjectSchema schema = generator.generateSchema(type).asObjectSchema();
            for (final Field field : type.getDeclaredFields()) {
            if (!field.getType().getName().startsWith("java") && !field.getType().isPrimitive()) {
            final ObjectSchema fieldSchema = generateSchema(generator, field.getType());
            fieldSchema.rejectAdditionalProperties();
            schema.putProperty(field.getName(), fieldSchema);
            }
            }
            return schema;
            }





            share|improve this answer















            You will need to specify the schema for each properties like:



            ObjectMapper mapper = new ObjectMapper();
            JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
            ObjectSchema schemaB = schemaGen.generateSchema(B.class).asObjectSchema();
            schemaB.rejectAdditionalProperties();

            ObjectSchema schema = schemaGen.generateSchema(A.class).asObjectSchema();
            schema.rejectAdditionalProperties();
            schema.putProperty("b", schemaB);


            You can leverage reflection api to automatically do it for you. Here is a quick and dirty example:



            public static void main(String args) throws JsonProcessingException {
            final ObjectMapper mapper = new ObjectMapper();
            final JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
            ObjectSchema schema = generateSchema(schemaGen, A.class);
            schema.rejectAdditionalProperties();
            System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
            }

            public static <T> ObjectSchema generateSchema(JsonSchemaGenerator generator, Class<T> type) throws JsonMappingException {
            ObjectSchema schema = generator.generateSchema(type).asObjectSchema();
            for (final Field field : type.getDeclaredFields()) {
            if (!field.getType().getName().startsWith("java") && !field.getType().isPrimitive()) {
            final ObjectSchema fieldSchema = generateSchema(generator, field.getType());
            fieldSchema.rejectAdditionalProperties();
            schema.putProperty(field.getName(), fieldSchema);
            }
            }
            return schema;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 28 '18 at 11:04

























            answered Feb 28 '18 at 10:30









            JEYJEY

            4,24512130




            4,24512130













            • Thanks for answer but I don't want to put properties manually. Does Jackson have any api to provide that?

              – Beno Arakelyan
              Feb 28 '18 at 10:47











            • In my answer i suggested to use java reflection API to do it automatically. I don't know any other way to do it.

              – JEY
              Feb 28 '18 at 10:50











            • I updated my answer with quick reflection usage.

              – JEY
              Feb 28 '18 at 11:05



















            • Thanks for answer but I don't want to put properties manually. Does Jackson have any api to provide that?

              – Beno Arakelyan
              Feb 28 '18 at 10:47











            • In my answer i suggested to use java reflection API to do it automatically. I don't know any other way to do it.

              – JEY
              Feb 28 '18 at 10:50











            • I updated my answer with quick reflection usage.

              – JEY
              Feb 28 '18 at 11:05

















            Thanks for answer but I don't want to put properties manually. Does Jackson have any api to provide that?

            – Beno Arakelyan
            Feb 28 '18 at 10:47





            Thanks for answer but I don't want to put properties manually. Does Jackson have any api to provide that?

            – Beno Arakelyan
            Feb 28 '18 at 10:47













            In my answer i suggested to use java reflection API to do it automatically. I don't know any other way to do it.

            – JEY
            Feb 28 '18 at 10:50





            In my answer i suggested to use java reflection API to do it automatically. I don't know any other way to do it.

            – JEY
            Feb 28 '18 at 10:50













            I updated my answer with quick reflection usage.

            – JEY
            Feb 28 '18 at 11:05





            I updated my answer with quick reflection usage.

            – JEY
            Feb 28 '18 at 11:05













            1














            The following worked for me:



            import com.fasterxml.jackson.databind.JsonNode;
            import com.fasterxml.jackson.databind.ObjectMapper;
            import com.kjetland.jackson.jsonSchema.JsonSchemaConfig;
            import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;

            ...

            ObjectMapper objectMapper = new ObjectMapper();
            JsonSchemaConfig config = JsonSchemaConfig.nullableJsonSchemaDraft4();
            JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper, config);
            JsonNode jsonNode = schemaGenerator.generateJsonSchema(Test.class);
            String jsonSchemaText = jsonNode.toString();


            Using maven dependency:



            <dependency>
            <groupId>com.kjetland</groupId>
            <artifactId>mbknor-jackson-jsonschema_2.12</artifactId>
            <version>1.0.28</version>
            </dependency>


            Using the following classes:



            Test.java:



            import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;

            public class Test {
            @JsonProperty(required = true)
            private final String name;
            private final TestChild child;

            @JsonCreator
            public Test (
            @JsonProperty("name") String name,
            @JsonProperty("child") TestChild child) {
            this.name = name;
            this.child = child;
            }

            public String getName () {
            return name;
            }

            public TestChild getChild () {
            return child;
            }
            }


            ...and TestChild.java:



            import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;

            public class TestChild {
            @JsonProperty(required = true)
            private final String childName;

            @JsonCreator
            public TestChild (@JsonProperty("childName") String childName) {
            this.childName = childName;
            }

            public String getChildName () {
            return childName;
            }
            }


            Results in (output of jsonSchemaText piped through jq -C . for pretty formatting):



            {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "title": "Test",
            "type": "object",
            "additionalProperties": false,
            "properties": {
            "name": {
            "type": "string"
            },
            "child": {
            "oneOf": [
            {
            "type": "null",
            "title": "Not included"
            },
            {
            "$ref": "#/definitions/TestChild"
            }
            ]
            }
            },
            "required": [
            "name"
            ],
            "definitions": {
            "TestChild": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
            "childName": {
            "type": "string"
            }
            },
            "required": [
            "childName"
            ]
            }
            }
            }


            This results in "additionalProperties": false on both Test and TestChild.



            Note: You can replace JsonSchemaConfig.nullableJsonSchemaDraft4() with JsonSchemaConfig.vanillaJsonSchemaDraft4() in your schema generation code to get rid of the "oneof" references with "type: null" or "type: ActualType" in favor of just "type: ActualType" (note, this still won't add them to the "required" array unless you annotate the properties with @JsonProperty(required = true)).






            share|improve this answer


























            • Notice this also results in "reference" based schemas, as opposed to flattened schemas (which won't allow self-referencing structures such as Trees where a Branch may contain Branches and/or Leaves) such as shown in the example provided in the question.

              – Shadow Man
              Mar 6 '18 at 2:30













            • Thanks. I will try your solution and let you know.

              – Beno Arakelyan
              Mar 7 '18 at 9:32











            • It is the scala library. Are there the java library which provides same functionality ?

              – Beno Arakelyan
              Mar 7 '18 at 23:37











            • I'm using this and I'm not using scala at all. I'm using pure java. The library may have been written in scala, but it certainly works in java. Personally, I don't see this as something to use in a service somewhere. I see this as something to run in a maven plugin. We wrote such a plugin at my work to generate the schema from our model classes as part of the maven build process and upload it to an S3 bucket, so we don't even pull in those libs to our generated jars.

              – Shadow Man
              Mar 8 '18 at 1:50


















            1














            The following worked for me:



            import com.fasterxml.jackson.databind.JsonNode;
            import com.fasterxml.jackson.databind.ObjectMapper;
            import com.kjetland.jackson.jsonSchema.JsonSchemaConfig;
            import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;

            ...

            ObjectMapper objectMapper = new ObjectMapper();
            JsonSchemaConfig config = JsonSchemaConfig.nullableJsonSchemaDraft4();
            JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper, config);
            JsonNode jsonNode = schemaGenerator.generateJsonSchema(Test.class);
            String jsonSchemaText = jsonNode.toString();


            Using maven dependency:



            <dependency>
            <groupId>com.kjetland</groupId>
            <artifactId>mbknor-jackson-jsonschema_2.12</artifactId>
            <version>1.0.28</version>
            </dependency>


            Using the following classes:



            Test.java:



            import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;

            public class Test {
            @JsonProperty(required = true)
            private final String name;
            private final TestChild child;

            @JsonCreator
            public Test (
            @JsonProperty("name") String name,
            @JsonProperty("child") TestChild child) {
            this.name = name;
            this.child = child;
            }

            public String getName () {
            return name;
            }

            public TestChild getChild () {
            return child;
            }
            }


            ...and TestChild.java:



            import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;

            public class TestChild {
            @JsonProperty(required = true)
            private final String childName;

            @JsonCreator
            public TestChild (@JsonProperty("childName") String childName) {
            this.childName = childName;
            }

            public String getChildName () {
            return childName;
            }
            }


            Results in (output of jsonSchemaText piped through jq -C . for pretty formatting):



            {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "title": "Test",
            "type": "object",
            "additionalProperties": false,
            "properties": {
            "name": {
            "type": "string"
            },
            "child": {
            "oneOf": [
            {
            "type": "null",
            "title": "Not included"
            },
            {
            "$ref": "#/definitions/TestChild"
            }
            ]
            }
            },
            "required": [
            "name"
            ],
            "definitions": {
            "TestChild": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
            "childName": {
            "type": "string"
            }
            },
            "required": [
            "childName"
            ]
            }
            }
            }


            This results in "additionalProperties": false on both Test and TestChild.



            Note: You can replace JsonSchemaConfig.nullableJsonSchemaDraft4() with JsonSchemaConfig.vanillaJsonSchemaDraft4() in your schema generation code to get rid of the "oneof" references with "type: null" or "type: ActualType" in favor of just "type: ActualType" (note, this still won't add them to the "required" array unless you annotate the properties with @JsonProperty(required = true)).






            share|improve this answer


























            • Notice this also results in "reference" based schemas, as opposed to flattened schemas (which won't allow self-referencing structures such as Trees where a Branch may contain Branches and/or Leaves) such as shown in the example provided in the question.

              – Shadow Man
              Mar 6 '18 at 2:30













            • Thanks. I will try your solution and let you know.

              – Beno Arakelyan
              Mar 7 '18 at 9:32











            • It is the scala library. Are there the java library which provides same functionality ?

              – Beno Arakelyan
              Mar 7 '18 at 23:37











            • I'm using this and I'm not using scala at all. I'm using pure java. The library may have been written in scala, but it certainly works in java. Personally, I don't see this as something to use in a service somewhere. I see this as something to run in a maven plugin. We wrote such a plugin at my work to generate the schema from our model classes as part of the maven build process and upload it to an S3 bucket, so we don't even pull in those libs to our generated jars.

              – Shadow Man
              Mar 8 '18 at 1:50
















            1












            1








            1







            The following worked for me:



            import com.fasterxml.jackson.databind.JsonNode;
            import com.fasterxml.jackson.databind.ObjectMapper;
            import com.kjetland.jackson.jsonSchema.JsonSchemaConfig;
            import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;

            ...

            ObjectMapper objectMapper = new ObjectMapper();
            JsonSchemaConfig config = JsonSchemaConfig.nullableJsonSchemaDraft4();
            JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper, config);
            JsonNode jsonNode = schemaGenerator.generateJsonSchema(Test.class);
            String jsonSchemaText = jsonNode.toString();


            Using maven dependency:



            <dependency>
            <groupId>com.kjetland</groupId>
            <artifactId>mbknor-jackson-jsonschema_2.12</artifactId>
            <version>1.0.28</version>
            </dependency>


            Using the following classes:



            Test.java:



            import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;

            public class Test {
            @JsonProperty(required = true)
            private final String name;
            private final TestChild child;

            @JsonCreator
            public Test (
            @JsonProperty("name") String name,
            @JsonProperty("child") TestChild child) {
            this.name = name;
            this.child = child;
            }

            public String getName () {
            return name;
            }

            public TestChild getChild () {
            return child;
            }
            }


            ...and TestChild.java:



            import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;

            public class TestChild {
            @JsonProperty(required = true)
            private final String childName;

            @JsonCreator
            public TestChild (@JsonProperty("childName") String childName) {
            this.childName = childName;
            }

            public String getChildName () {
            return childName;
            }
            }


            Results in (output of jsonSchemaText piped through jq -C . for pretty formatting):



            {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "title": "Test",
            "type": "object",
            "additionalProperties": false,
            "properties": {
            "name": {
            "type": "string"
            },
            "child": {
            "oneOf": [
            {
            "type": "null",
            "title": "Not included"
            },
            {
            "$ref": "#/definitions/TestChild"
            }
            ]
            }
            },
            "required": [
            "name"
            ],
            "definitions": {
            "TestChild": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
            "childName": {
            "type": "string"
            }
            },
            "required": [
            "childName"
            ]
            }
            }
            }


            This results in "additionalProperties": false on both Test and TestChild.



            Note: You can replace JsonSchemaConfig.nullableJsonSchemaDraft4() with JsonSchemaConfig.vanillaJsonSchemaDraft4() in your schema generation code to get rid of the "oneof" references with "type: null" or "type: ActualType" in favor of just "type: ActualType" (note, this still won't add them to the "required" array unless you annotate the properties with @JsonProperty(required = true)).






            share|improve this answer















            The following worked for me:



            import com.fasterxml.jackson.databind.JsonNode;
            import com.fasterxml.jackson.databind.ObjectMapper;
            import com.kjetland.jackson.jsonSchema.JsonSchemaConfig;
            import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;

            ...

            ObjectMapper objectMapper = new ObjectMapper();
            JsonSchemaConfig config = JsonSchemaConfig.nullableJsonSchemaDraft4();
            JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper, config);
            JsonNode jsonNode = schemaGenerator.generateJsonSchema(Test.class);
            String jsonSchemaText = jsonNode.toString();


            Using maven dependency:



            <dependency>
            <groupId>com.kjetland</groupId>
            <artifactId>mbknor-jackson-jsonschema_2.12</artifactId>
            <version>1.0.28</version>
            </dependency>


            Using the following classes:



            Test.java:



            import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;

            public class Test {
            @JsonProperty(required = true)
            private final String name;
            private final TestChild child;

            @JsonCreator
            public Test (
            @JsonProperty("name") String name,
            @JsonProperty("child") TestChild child) {
            this.name = name;
            this.child = child;
            }

            public String getName () {
            return name;
            }

            public TestChild getChild () {
            return child;
            }
            }


            ...and TestChild.java:



            import com.fasterxml.jackson.annotation.JsonCreator;
            import com.fasterxml.jackson.annotation.JsonProperty;

            public class TestChild {
            @JsonProperty(required = true)
            private final String childName;

            @JsonCreator
            public TestChild (@JsonProperty("childName") String childName) {
            this.childName = childName;
            }

            public String getChildName () {
            return childName;
            }
            }


            Results in (output of jsonSchemaText piped through jq -C . for pretty formatting):



            {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "title": "Test",
            "type": "object",
            "additionalProperties": false,
            "properties": {
            "name": {
            "type": "string"
            },
            "child": {
            "oneOf": [
            {
            "type": "null",
            "title": "Not included"
            },
            {
            "$ref": "#/definitions/TestChild"
            }
            ]
            }
            },
            "required": [
            "name"
            ],
            "definitions": {
            "TestChild": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
            "childName": {
            "type": "string"
            }
            },
            "required": [
            "childName"
            ]
            }
            }
            }


            This results in "additionalProperties": false on both Test and TestChild.



            Note: You can replace JsonSchemaConfig.nullableJsonSchemaDraft4() with JsonSchemaConfig.vanillaJsonSchemaDraft4() in your schema generation code to get rid of the "oneof" references with "type: null" or "type: ActualType" in favor of just "type: ActualType" (note, this still won't add them to the "required" array unless you annotate the properties with @JsonProperty(required = true)).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 6 '18 at 2:47

























            answered Mar 6 '18 at 2:14









            Shadow ManShadow Man

            2,1071325




            2,1071325













            • Notice this also results in "reference" based schemas, as opposed to flattened schemas (which won't allow self-referencing structures such as Trees where a Branch may contain Branches and/or Leaves) such as shown in the example provided in the question.

              – Shadow Man
              Mar 6 '18 at 2:30













            • Thanks. I will try your solution and let you know.

              – Beno Arakelyan
              Mar 7 '18 at 9:32











            • It is the scala library. Are there the java library which provides same functionality ?

              – Beno Arakelyan
              Mar 7 '18 at 23:37











            • I'm using this and I'm not using scala at all. I'm using pure java. The library may have been written in scala, but it certainly works in java. Personally, I don't see this as something to use in a service somewhere. I see this as something to run in a maven plugin. We wrote such a plugin at my work to generate the schema from our model classes as part of the maven build process and upload it to an S3 bucket, so we don't even pull in those libs to our generated jars.

              – Shadow Man
              Mar 8 '18 at 1:50





















            • Notice this also results in "reference" based schemas, as opposed to flattened schemas (which won't allow self-referencing structures such as Trees where a Branch may contain Branches and/or Leaves) such as shown in the example provided in the question.

              – Shadow Man
              Mar 6 '18 at 2:30













            • Thanks. I will try your solution and let you know.

              – Beno Arakelyan
              Mar 7 '18 at 9:32











            • It is the scala library. Are there the java library which provides same functionality ?

              – Beno Arakelyan
              Mar 7 '18 at 23:37











            • I'm using this and I'm not using scala at all. I'm using pure java. The library may have been written in scala, but it certainly works in java. Personally, I don't see this as something to use in a service somewhere. I see this as something to run in a maven plugin. We wrote such a plugin at my work to generate the schema from our model classes as part of the maven build process and upload it to an S3 bucket, so we don't even pull in those libs to our generated jars.

              – Shadow Man
              Mar 8 '18 at 1:50



















            Notice this also results in "reference" based schemas, as opposed to flattened schemas (which won't allow self-referencing structures such as Trees where a Branch may contain Branches and/or Leaves) such as shown in the example provided in the question.

            – Shadow Man
            Mar 6 '18 at 2:30







            Notice this also results in "reference" based schemas, as opposed to flattened schemas (which won't allow self-referencing structures such as Trees where a Branch may contain Branches and/or Leaves) such as shown in the example provided in the question.

            – Shadow Man
            Mar 6 '18 at 2:30















            Thanks. I will try your solution and let you know.

            – Beno Arakelyan
            Mar 7 '18 at 9:32





            Thanks. I will try your solution and let you know.

            – Beno Arakelyan
            Mar 7 '18 at 9:32













            It is the scala library. Are there the java library which provides same functionality ?

            – Beno Arakelyan
            Mar 7 '18 at 23:37





            It is the scala library. Are there the java library which provides same functionality ?

            – Beno Arakelyan
            Mar 7 '18 at 23:37













            I'm using this and I'm not using scala at all. I'm using pure java. The library may have been written in scala, but it certainly works in java. Personally, I don't see this as something to use in a service somewhere. I see this as something to run in a maven plugin. We wrote such a plugin at my work to generate the schema from our model classes as part of the maven build process and upload it to an S3 bucket, so we don't even pull in those libs to our generated jars.

            – Shadow Man
            Mar 8 '18 at 1:50







            I'm using this and I'm not using scala at all. I'm using pure java. The library may have been written in scala, but it certainly works in java. Personally, I don't see this as something to use in a service somewhere. I see this as something to run in a maven plugin. We wrote such a plugin at my work to generate the schema from our model classes as part of the maven build process and upload it to an S3 bucket, so we don't even pull in those libs to our generated jars.

            – Shadow Man
            Mar 8 '18 at 1:50













            0














            Well I would go to a simpler route if you don't want to use reflections. I would use JSONPath. So you would need to add below to your pom.xml



              <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.3.0</version>
            </dependency>


            Then below code demonstrates how to alter the generated JSON file



            package taruntest;

            import com.jayway.jsonpath.*;

            public class Test {

            public static void main(String args) throws Exception {
            String data = "{n" +
            " "type" : "object",n" +
            " "id" : "urn:jsonschema:com.xxx.xxx:A",n" +
            " "additionalProperties" : false,n" +
            " "properties" : {n" +
            " "s" : {n" +
            " "type" : "string"n" +
            " },n" +
            " "b" : {n" +
            " "type" : "object",n" +
            " "id" : "urn:jsonschema:com.xxx.xxx:B",n" +
            " "properties" : {n" +
            " "bd" : {n" +
            " "type" : "number"n" +
            " }n" +
            " }n" +
            " }n" +
            " }n" +
            "}";

            DocumentContext doc = JsonPath.parse(data);
            doc.put("$..[?(@.id =~ /urn:jsonschema:.*/)]", "additionalProperties", false);
            String modified = doc.jsonString();
            System.out.println(modified);
            }
            }


            The output of the run is (formatted manually)



            {
            "type": "object",
            "id": "urn:jsonschema:com.xxx.xxx:A",
            "additionalProperties": false,
            "properties": {
            "s": {
            "type": "string"
            },
            "b": {
            "type": "object",
            "id": "urn:jsonschema:com.xxx.xxx:B",
            "properties": {
            "bd": {
            "type": "number"
            }
            },
            "additionalProperties": false
            }
            }
            }





            share|improve this answer




























              0














              Well I would go to a simpler route if you don't want to use reflections. I would use JSONPath. So you would need to add below to your pom.xml



                <dependency>
              <groupId>com.jayway.jsonpath</groupId>
              <artifactId>json-path</artifactId>
              <version>2.3.0</version>
              </dependency>


              Then below code demonstrates how to alter the generated JSON file



              package taruntest;

              import com.jayway.jsonpath.*;

              public class Test {

              public static void main(String args) throws Exception {
              String data = "{n" +
              " "type" : "object",n" +
              " "id" : "urn:jsonschema:com.xxx.xxx:A",n" +
              " "additionalProperties" : false,n" +
              " "properties" : {n" +
              " "s" : {n" +
              " "type" : "string"n" +
              " },n" +
              " "b" : {n" +
              " "type" : "object",n" +
              " "id" : "urn:jsonschema:com.xxx.xxx:B",n" +
              " "properties" : {n" +
              " "bd" : {n" +
              " "type" : "number"n" +
              " }n" +
              " }n" +
              " }n" +
              " }n" +
              "}";

              DocumentContext doc = JsonPath.parse(data);
              doc.put("$..[?(@.id =~ /urn:jsonschema:.*/)]", "additionalProperties", false);
              String modified = doc.jsonString();
              System.out.println(modified);
              }
              }


              The output of the run is (formatted manually)



              {
              "type": "object",
              "id": "urn:jsonschema:com.xxx.xxx:A",
              "additionalProperties": false,
              "properties": {
              "s": {
              "type": "string"
              },
              "b": {
              "type": "object",
              "id": "urn:jsonschema:com.xxx.xxx:B",
              "properties": {
              "bd": {
              "type": "number"
              }
              },
              "additionalProperties": false
              }
              }
              }





              share|improve this answer


























                0












                0








                0







                Well I would go to a simpler route if you don't want to use reflections. I would use JSONPath. So you would need to add below to your pom.xml



                  <dependency>
                <groupId>com.jayway.jsonpath</groupId>
                <artifactId>json-path</artifactId>
                <version>2.3.0</version>
                </dependency>


                Then below code demonstrates how to alter the generated JSON file



                package taruntest;

                import com.jayway.jsonpath.*;

                public class Test {

                public static void main(String args) throws Exception {
                String data = "{n" +
                " "type" : "object",n" +
                " "id" : "urn:jsonschema:com.xxx.xxx:A",n" +
                " "additionalProperties" : false,n" +
                " "properties" : {n" +
                " "s" : {n" +
                " "type" : "string"n" +
                " },n" +
                " "b" : {n" +
                " "type" : "object",n" +
                " "id" : "urn:jsonschema:com.xxx.xxx:B",n" +
                " "properties" : {n" +
                " "bd" : {n" +
                " "type" : "number"n" +
                " }n" +
                " }n" +
                " }n" +
                " }n" +
                "}";

                DocumentContext doc = JsonPath.parse(data);
                doc.put("$..[?(@.id =~ /urn:jsonschema:.*/)]", "additionalProperties", false);
                String modified = doc.jsonString();
                System.out.println(modified);
                }
                }


                The output of the run is (formatted manually)



                {
                "type": "object",
                "id": "urn:jsonschema:com.xxx.xxx:A",
                "additionalProperties": false,
                "properties": {
                "s": {
                "type": "string"
                },
                "b": {
                "type": "object",
                "id": "urn:jsonschema:com.xxx.xxx:B",
                "properties": {
                "bd": {
                "type": "number"
                }
                },
                "additionalProperties": false
                }
                }
                }





                share|improve this answer













                Well I would go to a simpler route if you don't want to use reflections. I would use JSONPath. So you would need to add below to your pom.xml



                  <dependency>
                <groupId>com.jayway.jsonpath</groupId>
                <artifactId>json-path</artifactId>
                <version>2.3.0</version>
                </dependency>


                Then below code demonstrates how to alter the generated JSON file



                package taruntest;

                import com.jayway.jsonpath.*;

                public class Test {

                public static void main(String args) throws Exception {
                String data = "{n" +
                " "type" : "object",n" +
                " "id" : "urn:jsonschema:com.xxx.xxx:A",n" +
                " "additionalProperties" : false,n" +
                " "properties" : {n" +
                " "s" : {n" +
                " "type" : "string"n" +
                " },n" +
                " "b" : {n" +
                " "type" : "object",n" +
                " "id" : "urn:jsonschema:com.xxx.xxx:B",n" +
                " "properties" : {n" +
                " "bd" : {n" +
                " "type" : "number"n" +
                " }n" +
                " }n" +
                " }n" +
                " }n" +
                "}";

                DocumentContext doc = JsonPath.parse(data);
                doc.put("$..[?(@.id =~ /urn:jsonschema:.*/)]", "additionalProperties", false);
                String modified = doc.jsonString();
                System.out.println(modified);
                }
                }


                The output of the run is (formatted manually)



                {
                "type": "object",
                "id": "urn:jsonschema:com.xxx.xxx:A",
                "additionalProperties": false,
                "properties": {
                "s": {
                "type": "string"
                },
                "b": {
                "type": "object",
                "id": "urn:jsonschema:com.xxx.xxx:B",
                "properties": {
                "bd": {
                "type": "number"
                }
                },
                "additionalProperties": false
                }
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 28 '18 at 13:47









                Tarun LalwaniTarun Lalwani

                80.6k449112




                80.6k449112























                    -2














                    You can achieve this by impplementing Interface in those classes:



                    public interface I {
                    boolean additionalProperties = false;
                    }

                    public class A implements I {
                    private String s;
                    private B b;

                    public String getS() {
                    return s;
                    }

                    public B getB() {
                    return b;
                    }
                    }

                    public class B implements I {
                    private BigDecimal bd;

                    public BigDecimal getBd() {
                    return bd;
                    }
                    }





                    share|improve this answer



















                    • 1





                      Implement the interface which contains only properties is bad practice.

                      – Beno Arakelyan
                      Feb 28 '18 at 10:56













                    • @BenoArakelyan and JsonSchemaGenerator doesn't read the value of the property.

                      – JEY
                      Feb 28 '18 at 10:57











                    • sven.kwiotek It is mainly bad practice in java.

                      – Beno Arakelyan
                      Feb 28 '18 at 11:00
















                    -2














                    You can achieve this by impplementing Interface in those classes:



                    public interface I {
                    boolean additionalProperties = false;
                    }

                    public class A implements I {
                    private String s;
                    private B b;

                    public String getS() {
                    return s;
                    }

                    public B getB() {
                    return b;
                    }
                    }

                    public class B implements I {
                    private BigDecimal bd;

                    public BigDecimal getBd() {
                    return bd;
                    }
                    }





                    share|improve this answer



















                    • 1





                      Implement the interface which contains only properties is bad practice.

                      – Beno Arakelyan
                      Feb 28 '18 at 10:56













                    • @BenoArakelyan and JsonSchemaGenerator doesn't read the value of the property.

                      – JEY
                      Feb 28 '18 at 10:57











                    • sven.kwiotek It is mainly bad practice in java.

                      – Beno Arakelyan
                      Feb 28 '18 at 11:00














                    -2












                    -2








                    -2







                    You can achieve this by impplementing Interface in those classes:



                    public interface I {
                    boolean additionalProperties = false;
                    }

                    public class A implements I {
                    private String s;
                    private B b;

                    public String getS() {
                    return s;
                    }

                    public B getB() {
                    return b;
                    }
                    }

                    public class B implements I {
                    private BigDecimal bd;

                    public BigDecimal getBd() {
                    return bd;
                    }
                    }





                    share|improve this answer













                    You can achieve this by impplementing Interface in those classes:



                    public interface I {
                    boolean additionalProperties = false;
                    }

                    public class A implements I {
                    private String s;
                    private B b;

                    public String getS() {
                    return s;
                    }

                    public B getB() {
                    return b;
                    }
                    }

                    public class B implements I {
                    private BigDecimal bd;

                    public BigDecimal getBd() {
                    return bd;
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 28 '18 at 10:31









                    sven.kwioteksven.kwiotek

                    1,1251018




                    1,1251018








                    • 1





                      Implement the interface which contains only properties is bad practice.

                      – Beno Arakelyan
                      Feb 28 '18 at 10:56













                    • @BenoArakelyan and JsonSchemaGenerator doesn't read the value of the property.

                      – JEY
                      Feb 28 '18 at 10:57











                    • sven.kwiotek It is mainly bad practice in java.

                      – Beno Arakelyan
                      Feb 28 '18 at 11:00














                    • 1





                      Implement the interface which contains only properties is bad practice.

                      – Beno Arakelyan
                      Feb 28 '18 at 10:56













                    • @BenoArakelyan and JsonSchemaGenerator doesn't read the value of the property.

                      – JEY
                      Feb 28 '18 at 10:57











                    • sven.kwiotek It is mainly bad practice in java.

                      – Beno Arakelyan
                      Feb 28 '18 at 11:00








                    1




                    1





                    Implement the interface which contains only properties is bad practice.

                    – Beno Arakelyan
                    Feb 28 '18 at 10:56







                    Implement the interface which contains only properties is bad practice.

                    – Beno Arakelyan
                    Feb 28 '18 at 10:56















                    @BenoArakelyan and JsonSchemaGenerator doesn't read the value of the property.

                    – JEY
                    Feb 28 '18 at 10:57





                    @BenoArakelyan and JsonSchemaGenerator doesn't read the value of the property.

                    – JEY
                    Feb 28 '18 at 10:57













                    sven.kwiotek It is mainly bad practice in java.

                    – Beno Arakelyan
                    Feb 28 '18 at 11:00





                    sven.kwiotek It is mainly bad practice in java.

                    – Beno Arakelyan
                    Feb 28 '18 at 11:00


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48984259%2fjackson-how-can-i-generate-json-schema-which-rejects-all-additional-content%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python