Commit fddad974 authored by Morten Kjetland's avatar Morten Kjetland
Browse files

Added support for @Min and @Max

parent cdc4b067
Loading
Loading
Loading
Loading
+1 −9
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ Current version: *1.0.5*
* JSON Schema Draft v4
* Supports polymorphism using **@JsonTypeInfo** and **oneOf**
* Supports schema customization using **@JsonSchemaDescription**, **@JsonSchemaFormat** and **@JsonSchemaTitle**
* Supports many Javax-validation @Annotations
* Works well with Generated GUI's using [https://github.com/jdorn/json-editor](https://github.com/jdorn/json-editor)
  - (Must be configured to use this mode)
  - Special handling of Option-/Optional-properties using oneOf.
@@ -22,21 +23,12 @@ Current version: *1.0.5*
* Implemented in Scala 
* Easy to fix and add functionality

**The Future**

* Should support all different variations of Jackson-usage
* Should support all (a lot?) of *javax.validation-API*-annotations


Project status
---------------
We're currently using this codebase in an ongoing (not yet released) project at work,
and we're improving the jsonSchema-generating code when we finds issues and/or features we need that not yet is supported.

**Currently missing**

* Lot of javax.validation-API support

I would really appreciate it if other developers wanted to start using and contributing improvements and features. 

Dependency
+30 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import java.lang.reflect.{Field, Method, ParameterizedType}
import java.time.{LocalDate, LocalDateTime, LocalTime, OffsetDateTime}
import java.util
import java.util.Optional
import javax.validation.constraints.{NotNull, Size}
import javax.validation.constraints.{Max, Min, NotNull, Size}

import com.fasterxml.jackson.annotation.{JsonSubTypes, JsonTypeInfo}
import com.fasterxml.jackson.core.JsonParser.NumberType
@@ -298,6 +298,20 @@ class JsonSchemaGenerator

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

      // Look for @Min, @Max => minumum, maximum
      currentProperty.map {
        p =>
          Option(p.getAnnotation(classOf[Min])).map {
            min =>
              node.put("minimum", min.value())
          }

          Option(p.getAnnotation(classOf[Max])).map {
            max =>
              node.put("maximum", max.value())
          }
      }

      new JsonNumberFormatVisitor  with EnumSupport {
        val _node = node
        override def numberType(_type: NumberType): Unit = l(s"JsonNumberFormatVisitor.numberType: ${_type}")
@@ -323,6 +337,21 @@ class JsonSchemaGenerator

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

      // Look for @Min, @Max => minumum, maximum
      currentProperty.map {
        p =>
          Option(p.getAnnotation(classOf[Min])).map {
            min =>
              node.put("minimum", min.value())
          }

          Option(p.getAnnotation(classOf[Max])).map {
            max =>
              node.put("maximum", max.value())
          }
      }


      new JsonIntegerFormatVisitor with EnumSupport {
        val _node = node
        override def numberType(_type: NumberType): Unit = l(s"JsonIntegerFormatVisitor.numberType: ${_type}")
+19 −3
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ package com.kjetland.jackson.jsonSchema
import java.time.{LocalDate, LocalDateTime, OffsetDateTime}
import java.util
import java.util.{Optional, TimeZone}
import javax.validation.constraints.{NotNull, Size}
import javax.validation.constraints.{Max, Min, NotNull, Size}

import com.fasterxml.jackson.annotation.{JsonProperty, JsonSubTypes, JsonTypeInfo, JsonValue}
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
@@ -619,6 +619,12 @@ class JsonSchemaGeneratorTest extends FunSuite with Matchers {

    assert(schema.at("/properties/stringUsingSizeOnlyMax/maxLength").asInt() == 30)
    assert(schema.at("/properties/stringUsingSizeOnlyMax/minLength").isMissingNode == true)

    assert(schema.at("/properties/intMin/minimum").asInt() == 1)
    assert(schema.at("/properties/intMax/maximum").asInt() == 10)

    assert(schema.at("/properties/doubleMin/minimum").asInt() == 1)
    assert(schema.at("/properties/doubleMax/maximum").asInt() == 10)
  }

}
@@ -708,7 +714,7 @@ trait TestData {
  val pojoUsingFormat = new PojoUsingFormat("test@example.com", true, OffsetDateTime.now(), OffsetDateTime.now())
  val manyDates = ManyDates(LocalDateTime.now(), OffsetDateTime.now(), LocalDate.now(), org.joda.time.LocalDate.now())

  val classUsingValidation = ClassUsingValidation("_stringUsingNotNull", "_stringUsingSize", "_stringUsingSizeOnlyMin", "_stringUsingSizeOnlyMax" )
  val classUsingValidation = ClassUsingValidation("_stringUsingNotNull", "_stringUsingSize", "_stringUsingSizeOnlyMin", "_stringUsingSizeOnlyMax", 1, 2, 1.0, 2.0 )
}


@@ -776,6 +782,16 @@ case class ClassUsingValidation
  stringUsingSizeOnlyMin:String,

  @Size(max=30)
  stringUsingSizeOnlyMax:String
  stringUsingSizeOnlyMax:String,

  @Min(1)
  intMin:Int,
  @Max(10)
  intMax:Int,
  @Min(1)
  doubleMin:Double,
  @Max(10)
  doubleMax:Double


)
 No newline at end of file