Loading src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala +10 −5 Original line number Diff line number Diff line Loading @@ -31,7 +31,8 @@ object JsonSchemaConfig { hidePolymorphismTypeProperty = false, disableWarnings = false, useImprovedDateFormatMapping = false, useMinLengthForNotNull = false useMinLengthForNotNull = false, useTypeIdForDefinitionName = false ) /** Loading @@ -49,7 +50,8 @@ object JsonSchemaConfig { hidePolymorphismTypeProperty = true, disableWarnings = false, useImprovedDateFormatMapping = true, useMinLengthForNotNull = true useMinLengthForNotNull = true, useTypeIdForDefinitionName = false ) } Loading @@ -63,7 +65,8 @@ case class JsonSchemaConfig hidePolymorphismTypeProperty:Boolean, disableWarnings:Boolean, useImprovedDateFormatMapping:Boolean, useMinLengthForNotNull:Boolean useMinLengthForNotNull:Boolean, useTypeIdForDefinitionName:Boolean ) Loading Loading @@ -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 = { Loading @@ -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 Loading src/test/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGeneratorTest.scala +28 −1 Original line number Diff line number Diff line Loading @@ -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 = { Loading Loading @@ -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) Loading Loading
src/main/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGenerator.scala +10 −5 Original line number Diff line number Diff line Loading @@ -31,7 +31,8 @@ object JsonSchemaConfig { hidePolymorphismTypeProperty = false, disableWarnings = false, useImprovedDateFormatMapping = false, useMinLengthForNotNull = false useMinLengthForNotNull = false, useTypeIdForDefinitionName = false ) /** Loading @@ -49,7 +50,8 @@ object JsonSchemaConfig { hidePolymorphismTypeProperty = true, disableWarnings = false, useImprovedDateFormatMapping = true, useMinLengthForNotNull = true useMinLengthForNotNull = true, useTypeIdForDefinitionName = false ) } Loading @@ -63,7 +65,8 @@ case class JsonSchemaConfig hidePolymorphismTypeProperty:Boolean, disableWarnings:Boolean, useImprovedDateFormatMapping:Boolean, useMinLengthForNotNull:Boolean useMinLengthForNotNull:Boolean, useTypeIdForDefinitionName:Boolean ) Loading Loading @@ -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 = { Loading @@ -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 Loading
src/test/scala/com/kjetland/jackson/jsonSchema/JsonSchemaGeneratorTest.scala +28 −1 Original line number Diff line number Diff line Loading @@ -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 = { Loading Loading @@ -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) Loading