Commit f9809841 authored by mokj's avatar mokj
Browse files

Added support for enum

parent a36b82be
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@
lazy val commonSettings = Seq(
  organization := "com.kjetland",
  organizationName := "mbknor",
  version := "1.0.0-SNAPSHOT",
  version := "1.0.0-build-2-SNAPSHOT",
  scalaVersion := "2.11.8",
  publishMavenStyle := true,
  publishArtifact in Test := false,
+31 −11
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@ import org.slf4j.LoggerFactory

class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {

  import scala.collection.JavaConversions._

  val log = LoggerFactory.getLogger(getClass)

  trait MySerializerProvider {
@@ -23,6 +25,23 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {

  }

  trait EnumSupport {

    val _node: ObjectNode

    def enumTypes(enums: util.Set[String]): Unit = {
      // l(s"JsonStringFormatVisitor-enum.enumTypes: ${enums}")

      val enumValuesNode = JsonNodeFactory.instance.arrayNode()
      _node.set("enum", enumValuesNode)

      enums.toSet[String].foreach {
        enumValue =>
          enumValuesNode.add(enumValue)
      }
    }
  }


  case class SubTypeAndTypeName[T](clazz: Class[T], subTypeName: String)

@@ -35,14 +54,16 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {
    def createChild(childNode: ObjectNode): MyJsonFormatVisitorWrapper = new MyJsonFormatVisitorWrapper(objectMapper, indent + "   ", childNode)

    override def expectStringFormat(_type: JavaType) = {
      l("expectStringFormat")
      l(s"expectStringFormat - _type: ${_type}")

      node.put("type", "string")

      new JsonStringFormatVisitor {
        override def enumTypes(enums: util.Set[String]): Unit = l(s"JsonStringFormatVisitor.enumTypes: ${enums}")
      new JsonStringFormatVisitor with EnumSupport {
        val _node = node
        override def format(format: JsonValueFormat): Unit = l(s"JsonStringFormatVisitor.format: ${format}")
      }


    }

    override def expectArrayFormat(_type: JavaType) = {
@@ -62,16 +83,15 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {

      node.put("type", "number")

      new JsonNumberFormatVisitor {
      new JsonNumberFormatVisitor  with EnumSupport {
        val _node = node
        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}")
      }
    }

    override def expectAnyFormat(_type: JavaType) = {
      l("expectAnyFormat")
      ???
      log.warn(s"Not able to generate jsonSchema-info for type: ${_type} - probably using custom serializer which does not override acceptJsonFormatVisitor")
      new JsonAnyFormatVisitor {
      }

@@ -82,9 +102,9 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {

      node.put("type", "integer")

      new JsonIntegerFormatVisitor {
      new JsonIntegerFormatVisitor with EnumSupport {
        val _node = node
        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}")
      }
    }
@@ -100,8 +120,8 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {

      node.put("type", "boolean")

      new JsonBooleanFormatVisitor {
        override def enumTypes(enums: util.Set[String]): Unit = l(s"JsonBooleanFormatVisitor.enumTypes: ${enums}")
      new JsonBooleanFormatVisitor with EnumSupport {
        val _node = node
        override def format(format: JsonValueFormat): Unit = l(s"JsonBooleanFormatVisitor.format: ${format}")
      }
    }
+20 −2
Original line number Diff line number Diff line
package com.kjetland.jackson.jsonSchema

import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.github.fge.jsonschema.main.JsonSchemaFactory
import com.kjetland.jackson.jsonSchema.testData.{Child1, ManyPrimitives, Parent, PojoWithParent}
import com.kjetland.jackson.jsonSchema.testData._
import org.scalatest.{FunSuite, Matchers}

class JsonSchemaGeneratorTest extends FunSuite with Matchers with TestData {

  val objectMapper = new ObjectMapper()
  val simpleModule = new SimpleModule()
  simpleModule.addSerializer(classOf[PojoWithCustomSerializer], new PojoWithCustomSerializerSerializer)
  simpleModule.addDeserializer(classOf[PojoWithCustomSerializer], new PojoWithCustomSerializerDeserializer)
  objectMapper.registerModule(simpleModule)

  val jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper)

@@ -74,6 +79,14 @@ class JsonSchemaGeneratorTest extends FunSuite with Matchers with TestData {
    println(schemaAsJson)
  }

  test("custom serializer not overriding JsonSerializer.acceptJsonFormatVisitor") {

    val jsonNode = assertToFromJson(pojoWithCustomSerializer)
    val schemaAsJson = generateAndValidateSchema(pojoWithCustomSerializer.getClass, Some(jsonNode))
    println("--------------------------------------------")
    println(schemaAsJson)
  }


}

@@ -92,6 +105,11 @@ trait TestData {
    p
  }

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

  val pojoWithCustomSerializer = {
    val p = new PojoWithCustomSerializer
    p.myString = "xxx"
    p
  }
}
+7 −3
Original line number Diff line number Diff line
@@ -8,11 +8,12 @@ public class ManyPrimitives {
    public boolean _boolean2;
    public Double _double;
    public double _double2;
    public MyEnum myEnum;

    public ManyPrimitives() {
    }

    public ManyPrimitives(String _string, Integer _integer, int _int, Boolean _boolean, boolean _boolean2, Double _double, double _double2) {
    public ManyPrimitives(String _string, Integer _integer, int _int, Boolean _boolean, boolean _boolean2, Double _double, double _double2, MyEnum myEnum) {
        this._string = _string;
        this._integer = _integer;
        this._int = _int;
@@ -20,12 +21,13 @@ public class ManyPrimitives {
        this._boolean2 = _boolean2;
        this._double = _double;
        this._double2 = _double2;
        this.myEnum = myEnum;
    }

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

        ManyPrimitives that = (ManyPrimitives) o;

@@ -35,7 +37,8 @@ public class ManyPrimitives {
        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;
        if (_double != null ? !_double.equals(that._double) : that._double != null) return false;
        return myEnum == that.myEnum;

    }

@@ -51,6 +54,7 @@ public class ManyPrimitives {
        result = 31 * result + (_double != null ? _double.hashCode() : 0);
        temp = Double.doubleToLongBits(_double2);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (myEnum != null ? myEnum.hashCode() : 0);
        return result;
    }
}
+5 −0
Original line number Diff line number Diff line
package com.kjetland.jackson.jsonSchema.testData;

public enum MyEnum {
    A,B,C
}
Loading