Commit a702282f authored by Morten Kjetland's avatar Morten Kjetland Committed by GitHub
Browse files

Merge pull request #15 from cyclops23/master

Allow use of fully qualified class names in refs
parents cded3f56 2d4ca052
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -31,7 +31,8 @@ object JsonSchemaConfig {
    hidePolymorphismTypeProperty = false,
    disableWarnings = false,
    useImprovedDateFormatMapping = false,
    useMinLengthForNotNull = false
    useMinLengthForNotNull = false,
    useTypeIdForDefinitionName = false
  )

  /**
@@ -49,7 +50,8 @@ object JsonSchemaConfig {
    hidePolymorphismTypeProperty = true,
    disableWarnings = false,
    useImprovedDateFormatMapping = true,
    useMinLengthForNotNull = true
    useMinLengthForNotNull = true,
    useTypeIdForDefinitionName = false
  )

}
@@ -63,7 +65,8 @@ case class JsonSchemaConfig
  hidePolymorphismTypeProperty:Boolean,
  disableWarnings:Boolean,
  useImprovedDateFormatMapping:Boolean,
  useMinLengthForNotNull:Boolean
  useMinLengthForNotNull:Boolean,
  useTypeIdForDefinitionName:Boolean
)


@@ -143,6 +146,8 @@ class JsonSchemaGenerator
    // Used when 'combining' multiple invocations to getOrCreateDefinition when processing polymorphism.
    var workInProgress:Option[WorkInProgress] = None

    def getDefinitionName (clazz:Class[_]) = { if (config.useTypeIdForDefinitionName) clazz.getName else clazz.getSimpleName }

    // Either creates new definitions or return $ref to existing one
    def getOrCreateDefinition(clazz:Class[_])(objectDefinitionBuilder:(ObjectNode) => Option[JsonObjectFormatVisitor]):DefinitionInfo = {

@@ -164,8 +169,8 @@ class JsonSchemaGenerator

          // new one - must build it
          var retryCount = 0
          var shortRef = clazz.getSimpleName
          var longRef = "#/definitions/"+clazz.getSimpleName
          var shortRef = getDefinitionName(clazz)
          var longRef = "#/definitions/" + shortRef
          while( class2Ref.values.contains(longRef)) {
            retryCount = retryCount + 1
            shortRef = clazz.getSimpleName + "_" + retryCount
+28 −1
Original line number Diff line number Diff line
@@ -52,6 +52,19 @@ class JsonSchemaGeneratorTest extends FunSuite with Matchers {
  val jsonSchemaGeneratorScala = new JsonSchemaGenerator(_objectMapperScala, debug = true)
  val jsonSchemaGeneratorScalaHTML5 = new JsonSchemaGenerator(_objectMapperScala, debug = true, config = JsonSchemaConfig.html5EnabledSchema)

  val vanillaJsonSchemaDraft4WithIds = JsonSchemaConfig(
    autoGenerateTitleForProperties = true,
    defaultArrayFormat = Some("table"),
    useOneOfForOption = true,
    usePropertyOrdering = true,
    hidePolymorphismTypeProperty = true,
    disableWarnings = false,
    useImprovedDateFormatMapping = true,
    useMinLengthForNotNull = true,
    useTypeIdForDefinitionName = true
  )
  val jsonSchemaGeneratorWithIds = new JsonSchemaGenerator(_objectMapperScala, debug = true, vanillaJsonSchemaDraft4WithIds)

  val testData = new TestData{}

  def asPrettyJson(node:JsonNode, om:ObjectMapper):String = {
@@ -226,6 +239,20 @@ class JsonSchemaGeneratorTest extends FunSuite with Matchers {

    }

    //Using fully-qualified class names
    {

      val jsonNode = assertToFromJson(jsonSchemaGeneratorWithIds, testData.pojoWithParent)
      val schema = generateAndValidateSchema(jsonSchemaGeneratorWithIds, testData.pojoWithParent.getClass, Some(jsonNode))

      assert(false == schema.at("/additionalProperties").asBoolean())
      assert(schema.at("/properties/pojoValue/type").asText() == "boolean")

      assertChild1(schema, "/properties/child/oneOf", "com.kjetland.jackson.jsonSchema.testData.Child1", false)
      assertChild2(schema, "/properties/child/oneOf", "com.kjetland.jackson.jsonSchema.testData.Child2", false)
      
    }
    
    // Scala
    {
      val jsonNode = assertToFromJson(jsonSchemaGeneratorScala, testData.pojoWithParentScala)