Commit 4822d821 authored by Morten Kjetland's avatar Morten Kjetland
Browse files

Added minimum map-support

parent 507ccc01
Loading
Loading
Loading
Loading
+18 −7
Original line number Diff line number Diff line
@@ -190,9 +190,9 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper, debug:Boolean = false)
      }
    }

    override def expectNullFormat(_type: JavaType) = new JsonNullFormatVisitor {
      l("expectNullFormat")
      ???
    override def expectNullFormat(_type: JavaType) = {
      l(s"expectNullFormat - _type: ${_type}")
      new JsonNullFormatVisitor {}
    }


@@ -208,14 +208,25 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper, debug:Boolean = false)
    }

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

      // There is no way to specify map in jsonSchema,
      // So we're going to treat it as type=object with additionalProperties = true,
      // so that it can hold whatever the map can hold


      node.put("type", "object")
      node.put("additionalProperties", true)

      ???

      new JsonMapFormatVisitor with MySerializerProvider {
        override def keyFormat(handler: JsonFormatVisitable, keyType: JavaType): Unit = ???
        override def keyFormat(handler: JsonFormatVisitable, keyType: JavaType): Unit = {
          l(s"JsonMapFormatVisitor.keyFormat handler: $handler - keyType: $keyType")
        }

        override def valueFormat(handler: JsonFormatVisitable, valueType: JavaType): Unit = ???
        override def valueFormat(handler: JsonFormatVisitable, valueType: JavaType): Unit = {
          l(s"JsonMapFormatVisitor.valueFormat handler: $handler - valueType: $valueType")
        }
      }
    }

+22 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ class JsonSchemaGeneratorTest extends FunSuite with Matchers {
  simpleModule.addDeserializer(classOf[PojoWithCustomSerializer], new PojoWithCustomSerializerDeserializer)
  objectMapper.registerModule(simpleModule)

  val jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper)
  val jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper, debug = true)

  val testData = new TestData{}

@@ -263,6 +263,20 @@ class JsonSchemaGeneratorTest extends FunSuite with Matchers {

  }

  test("pojo using Maps") {
    val jsonNode = assertToFromJson(testData.pojoUsingMaps)
    val schema = generateAndValidateSchema(testData.pojoUsingMaps.getClass, Some(jsonNode))

    assert( schema.at("/properties/string2Integer/type").asText() == "object")
    assert( schema.at("/properties/string2Integer/additionalProperties").asBoolean() == true)

    assert( schema.at("/properties/string2String/type").asText() == "object")
    assert( schema.at("/properties/string2String/additionalProperties").asBoolean() == true)

    assert( schema.at("/properties/string2PojoUsingJsonTypeInfo/type").asText() == "object")
    assert( schema.at("/properties/string2PojoUsingJsonTypeInfo/additionalProperties").asBoolean() == true)
  }


}

@@ -315,4 +329,11 @@ trait TestData {
  )

  val recursivePojo = new RecursivePojo("t1", List(new RecursivePojo("c1", null)))

  val pojoUsingMaps = new PojoUsingMaps(
      Map[String, Integer]("a" -> 1, "b" -> 2),
      Map("x" -> "y", "z" -> "w"),
      Map[String, Parent]("1" -> child1, "2" -> child2)
    )

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

import java.util.Map;

public class PojoUsingMaps {

    public Map<String, Integer> string2Integer;
    public Map<String, String> string2String;
    public Map<String, Parent> string2PojoUsingJsonTypeInfo;

    public PojoUsingMaps() {
    }

    public PojoUsingMaps(Map<String, Integer> string2Integer, Map<String, String> string2String, Map<String, Parent> string2PojoUsingJsonTypeInfo) {
        this.string2Integer = string2Integer;
        this.string2String = string2String;
        this.string2PojoUsingJsonTypeInfo = string2PojoUsingJsonTypeInfo;
    }

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

        PojoUsingMaps that = (PojoUsingMaps) o;

        if (string2Integer != null ? !string2Integer.equals(that.string2Integer) : that.string2Integer != null)
            return false;
        if (string2String != null ? !string2String.equals(that.string2String) : that.string2String != null)
            return false;
        return string2PojoUsingJsonTypeInfo != null ? string2PojoUsingJsonTypeInfo.equals(that.string2PojoUsingJsonTypeInfo) : that.string2PojoUsingJsonTypeInfo == null;

    }

    @Override
    public int hashCode() {
        int result = string2Integer != null ? string2Integer.hashCode() : 0;
        result = 31 * result + (string2String != null ? string2String.hashCode() : 0);
        result = 31 * result + (string2PojoUsingJsonTypeInfo != null ? string2PojoUsingJsonTypeInfo.hashCode() : 0);
        return result;
    }
}