Commit a36b82be authored by Morten Kjetland's avatar Morten Kjetland
Browse files

More code

parent 1000c49e
Loading
Loading
Loading
Loading
+28 −24
Original line number Diff line number Diff line
@@ -40,9 +40,8 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {
      node.put("type", "string")

      new JsonStringFormatVisitor {
        override def enumTypes(enums: util.Set[String]): Unit = l(s"expectStringFormat - enums: $enums")

        override def format(format: JsonValueFormat): Unit = l(s"expectStringFormat - format: $format")
        override def enumTypes(enums: util.Set[String]): Unit = l(s"JsonStringFormatVisitor.enumTypes: ${enums}")
        override def format(format: JsonValueFormat): Unit = l(s"JsonStringFormatVisitor.format: ${format}")
      }
    }

@@ -64,11 +63,9 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {
      node.put("type", "number")

      new JsonNumberFormatVisitor {
        override def numberType(_type: NumberType): Unit = ???

        override def enumTypes(enums: util.Set[String]): Unit = ???

        override def format(format: JsonValueFormat): Unit = ???
        override def numberType(_type: NumberType): Unit = l(s"JsonNumberFormatVisitor.numberType: ${_type}")
        override def enumTypes(enums: util.Set[String]): Unit = l(s"JsonNumberFormatVisitor.enumTypes: ${enums}")
        override def format(format: JsonValueFormat): Unit = l(s"JsonNumberFormatVisitor.format: ${format}")
      }
    }

@@ -86,11 +83,9 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {
      node.put("type", "integer")

      new JsonIntegerFormatVisitor {
        override def numberType(_type: NumberType): Unit = l(s"expectIntegerFormat - type = ${_type}")

        override def enumTypes(enums: util.Set[String]): Unit = ???

        override def format(format: JsonValueFormat): Unit = ???
        override def numberType(_type: NumberType): Unit = l(s"JsonIntegerFormatVisitor.numberType: ${_type}")
        override def enumTypes(enums: util.Set[String]): Unit = l(s"JsonIntegerFormatVisitor.enumTypes: ${enums}")
        override def format(format: JsonValueFormat): Unit = l(s"JsonIntegerFormatVisitor.format: ${format}")
      }
    }

@@ -106,9 +101,8 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {
      node.put("type", "boolean")

      new JsonBooleanFormatVisitor {
        override def enumTypes(enums: util.Set[String]): Unit = ???

        override def format(format: JsonValueFormat): Unit = ???
        override def enumTypes(enums: util.Set[String]): Unit = l(s"JsonBooleanFormatVisitor.enumTypes: ${enums}")
        override def format(format: JsonValueFormat): Unit = l(s"JsonBooleanFormatVisitor.format: ${format}")
      }
    }

@@ -138,7 +132,7 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {
        //l(s"polymorphism - subTypes: $subTypes")

        val anyOfArrayNode = JsonNodeFactory.instance.arrayNode()
        node.set("anyOf", anyOfArrayNode)
        node.set("oneOf", anyOfArrayNode)

        val subTypeSpecifierPropertyName: String = _type.getRawClass.getDeclaredAnnotation(classOf[JsonTypeInfo]).property()

@@ -146,20 +140,26 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {
          subType: SubTypeAndTypeName[_] =>
            l(s"polymorphism - subType: $subType")

            val thisAnyOfNode = JsonNodeFactory.instance.objectNode()
            anyOfArrayNode.add(thisAnyOfNode)
            val thisOneOfNode = JsonNodeFactory.instance.objectNode()

            val childVisitor = createChild(thisAnyOfNode)
            // Set the title = subTypeName
            thisOneOfNode.put("title", subType.subTypeName)

            anyOfArrayNode.add(thisOneOfNode)

            val childVisitor = createChild(thisOneOfNode)
            objectMapper.acceptJsonFormatVisitor(subType.clazz, childVisitor)

            // must inject the 'type'-param and value as enum with only one possible value
            val propertiesNode = thisAnyOfNode.get("properties").asInstanceOf[ObjectNode]
            val propertiesNode = thisOneOfNode.get("properties").asInstanceOf[ObjectNode]

            val enumValuesNode = JsonNodeFactory.instance.arrayNode()
            enumValuesNode.add(subType.subTypeName)

            val enumObjectNode = JsonNodeFactory.instance.objectNode()
            enumObjectNode.set("enum", enumValuesNode)
            enumObjectNode.put("default", subType.subTypeName)


            propertiesNode.set(subTypeSpecifierPropertyName, enumObjectNode)
        }
@@ -187,11 +187,15 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {

          }

          override def optionalProperty(name: String, handler: JsonFormatVisitable, propertyTypeHint: JavaType): Unit = ???
          override def optionalProperty(name: String, handler: JsonFormatVisitable, propertyTypeHint: JavaType): Unit = {
            l(s"JsonObjectFormatVisitor.optionalProperty: name:${name} handler:${handler} propertyTypeHint:${propertyTypeHint}")
          }

          override def property(writer: BeanProperty): Unit = ???
          override def property(writer: BeanProperty): Unit = l(s"JsonObjectFormatVisitor.property: name:${writer}")

          override def property(name: String, handler: JsonFormatVisitable, propertyTypeHint: JavaType): Unit = ???
          override def property(name: String, handler: JsonFormatVisitable, propertyTypeHint: JavaType): Unit = {
            l(s"JsonObjectFormatVisitor.property: name:${name} handler:${handler} propertyTypeHint:${propertyTypeHint}")
          }
        }

      }
+11 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ package com.kjetland.jackson.jsonSchema
import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.github.fge.jsonschema.main.JsonSchemaFactory
import com.kjetland.jackson.jsonSchema.testData.{Child1, Parent, PojoWithParent}
import com.kjetland.jackson.jsonSchema.testData.{Child1, ManyPrimitives, Parent, PojoWithParent}
import org.scalatest.{FunSuite, Matchers}

class JsonSchemaGeneratorTest extends FunSuite with Matchers with TestData {
@@ -67,6 +67,13 @@ class JsonSchemaGeneratorTest extends FunSuite with Matchers with TestData {
    println(schemaAsJson)
  }

  test("primitives") {
    val jsonNode = assertToFromJson(manyPrimitives)
    val schemaAsJson = generateAndValidateSchema(manyPrimitives.getClass, Some(jsonNode))
    println("--------------------------------------------")
    println(schemaAsJson)
  }


}

@@ -84,4 +91,7 @@ trait TestData {
    p.child = child1
    p
  }

  val manyPrimitives = new ManyPrimitives("s1", 1, 2, true, false, 0.1, 0.2)

}
+1 −1
Original line number Diff line number Diff line
package com.kjetland.jackson.jsonSchema.testData;

public class Child2 {
public class Child2 extends Parent {

    public Integer child2int;

+56 −0
Original line number Diff line number Diff line
package com.kjetland.jackson.jsonSchema.testData;

public class ManyPrimitives {
    public String _string;
    public Integer _integer;
    public int _int;
    public Boolean _boolean;
    public boolean _boolean2;
    public Double _double;
    public double _double2;

    public ManyPrimitives() {
    }

    public ManyPrimitives(String _string, Integer _integer, int _int, Boolean _boolean, boolean _boolean2, Double _double, double _double2) {
        this._string = _string;
        this._integer = _integer;
        this._int = _int;
        this._boolean = _boolean;
        this._boolean2 = _boolean2;
        this._double = _double;
        this._double2 = _double2;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ManyPrimitives that = (ManyPrimitives) o;

        if (_int != that._int) return false;
        if (_boolean2 != that._boolean2) return false;
        if (Double.compare(that._double2, _double2) != 0) return false;
        if (_string != null ? !_string.equals(that._string) : that._string != null) return false;
        if (_integer != null ? !_integer.equals(that._integer) : that._integer != null) return false;
        if (_boolean != null ? !_boolean.equals(that._boolean) : that._boolean != null) return false;
        return _double != null ? _double.equals(that._double) : that._double == null;

    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = _string != null ? _string.hashCode() : 0;
        result = 31 * result + (_integer != null ? _integer.hashCode() : 0);
        result = 31 * result + _int;
        result = 31 * result + (_boolean != null ? _boolean.hashCode() : 0);
        result = 31 * result + (_boolean2 ? 1 : 0);
        result = 31 * result + (_double != null ? _double.hashCode() : 0);
        temp = Double.doubleToLongBits(_double2);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
}