Skip to content

jsonSchema generation for classes extending TraversableOnce return { type: array } #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kolia opened this issue Jan 6, 2016 · 1 comment

Comments

@kolia
Copy link

kolia commented Jan 6, 2016

The jsonSchema returned for:

  case class WorkingClass(int: Int) {
    def iterator: Iterator[Int] = List().toIterator
  }

looks correct:

{
  "type" : "object",
  "id" : "urn:jsonschema:com:ccri:commons:config:schema:ReproduceIssue:WorkingClass",
  "properties" : {
    "int" : {
      "type" : "integer"
    }
  }
}

But if the class is declared as extending Iterable[Int]

  case class FailingClass(int: Int) extends Iterable[Int] {
    def iterator: Iterator[Int] = List().toIterator
  }

I get:

{
  "type" : "array"
}

As far as I can tell, mixing in any trait that subclasses TraversableOnce produces {"type": "array"}.

To reproduce:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.jsonSchema.customProperties.ValidationSchemaFactoryWrapper
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper


object JacksonHelper {
  val jacksonObjectMapper: ObjectMapper = new ObjectMapper() with ScalaObjectMapper
  jacksonObjectMapper.registerModule(DefaultScalaModule)

  val writer = jacksonObjectMapper.writer()

  val visitor: SchemaFactoryWrapper = new ValidationSchemaFactoryWrapper()

  val prettyPrinter = jacksonObjectMapper.writerWithDefaultPrettyPrinter()

  def jsonSchema(c: Class[_]) = {
    writer.acceptJsonFormatVisitor(c, visitor)
    val jsonSchema = visitor.finalSchema()
    prettyPrinter.writeValueAsString(jsonSchema)
  }
}

object ReproduceIssue extends App {

  case class WorkingClass(int: Int) {
    def iterator: Iterator[Int] = List().toIterator
  }

  case class FailingClass(int: Int) extends Iterable[Int] {
    def iterator: Iterator[Int] = List().toIterator
  }

  println("WorkingClass:")
  println(JacksonHelper.jsonSchema(classOf[WorkingClass]))
  println("FailingClass:")
  println(JacksonHelper.jsonSchema(classOf[FailingClass]))
}
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jsonSchema</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-scala_2.10</artifactId>
            <version>2.6.3</version>
        </dependency>

Am I doing something wrong, is this not supposed to work, or is it a bug?

@pjfanning
Copy link
Member

jackson-module-jsonSchema support is deprecated and due for removal when jackson 3 is released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants