Commit 21dd47ff authored by mokj's avatar mokj
Browse files

more on arrays

parent f9809841
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-build-2-SNAPSHOT",
  version := "1.0.0-build-3-SNAPSHOT",
  scalaVersion := "2.11.8",
  publishMavenStyle := true,
  publishArtifact in Test := false,
+12 −3
Original line number Diff line number Diff line
@@ -67,14 +67,23 @@ class JsonSchemaGenerator(rootObjectMapper: ObjectMapper) {
    }

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

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

      val itemsNode = JsonNodeFactory.instance.objectNode()
      node.set("items", itemsNode)

      new JsonArrayFormatVisitor with MySerializerProvider {
        override def itemsFormat(handler: JsonFormatVisitable, elementType: JavaType): Unit = l(s"expectArrayFormat - handler: $handler - elementType: $elementType")
        override def itemsFormat(handler: JsonFormatVisitable, elementType: JavaType): Unit = {
          l(s"expectArrayFormat - handler: $handler - elementType: $elementType")
          objectMapper.acceptJsonFormatVisitor(elementType, createChild(itemsNode))
        }

        override def itemsFormat(format: JsonFormatTypes): Unit = l(s"itemsFormat - format: $format")
        override def itemsFormat(format: JsonFormatTypes): Unit = {
          l(s"itemsFormat - format: $format")
          itemsNode.put("type", format.value())
        }
      }
    }

+25 −0
Original line number Diff line number Diff line
@@ -87,10 +87,19 @@ class JsonSchemaGeneratorTest extends FunSuite with Matchers with TestData {
    println(schemaAsJson)
  }

  test("pojoWithArrays") {

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


}

trait TestData {
  import scala.collection.JavaConversions._
  val child1 = {
    val c = new Child1()
    c.parentString = "pv"
@@ -98,6 +107,13 @@ trait TestData {
    c
  }

  val child2 = {
    val c = new Child2()
    c.parentString = "pv"
    c.child2int = 12
    c
  }

  val pojoWithParent = {
    val p = new PojoWithParent
    p.pojoValue = true
@@ -112,4 +128,13 @@ trait TestData {
    p.myString = "xxx"
    p
  }

  val pojoWithArrays = new PojoWithArrays(
    Array(1,2,3),
    Array("a1","a2","a3"),
    List("l1", "l2", "l3"),
    List(child1, child2),
    List(child1, child2).toArray

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

import java.util.Arrays;
import java.util.List;

public class PojoWithArrays {

    public int[] intArray1;
    public String[] stringArray;

    public List<String> stringList;

    public List<Parent> polymorphismList;
    public Parent[] polymorphismArray;

    public PojoWithArrays() {
    }

    public PojoWithArrays(int[] intArray1, String[] stringArray, List<String> stringList, List<Parent> polymorphismList, Parent[] polymorphismArray) {
        this.intArray1 = intArray1;
        this.stringArray = stringArray;
        this.stringList = stringList;
        this.polymorphismList = polymorphismList;
        this.polymorphismArray = polymorphismArray;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PojoWithArrays)) return false;

        PojoWithArrays that = (PojoWithArrays) o;

        if (!Arrays.equals(intArray1, that.intArray1)) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        if (!Arrays.equals(stringArray, that.stringArray)) return false;
        if (stringList != null ? !stringList.equals(that.stringList) : that.stringList != null) return false;
        if (polymorphismList != null ? !polymorphismList.equals(that.polymorphismList) : that.polymorphismList != null)
            return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        return Arrays.equals(polymorphismArray, that.polymorphismArray);

    }

    @Override
    public int hashCode() {
        int result = Arrays.hashCode(intArray1);
        result = 31 * result + Arrays.hashCode(stringArray);
        result = 31 * result + (stringList != null ? stringList.hashCode() : 0);
        result = 31 * result + (polymorphismList != null ? polymorphismList.hashCode() : 0);
        result = 31 * result + Arrays.hashCode(polymorphismArray);
        return result;
    }
}
+23 −23

File changed.

Contains only whitespace changes.

+16 −16

File changed.

Contains only whitespace changes.

+5 −5

File changed.

Contains only whitespace changes.

Loading