andyglow / scala-jsonschema

Scala JSON Schema
Other
123 stars 38 forks source link

With Either type schema is not correct #220

Open kokorin opened 3 years ago

kokorin commented 3 years ago

Describe the bug With Either[String, Person] schema is generated with extra properties a and b

To Reproduce

package eithersupport

import com.github.andyglow.json.JsonFormatter
import com.github.andyglow.jsonschema.AsValue
import json.Json
import json.schema.Version
import org.scalatest.funsuite.AnyFunSuite

case class Person(name: String, nickname: String)

case class People(people: Seq[Either[String, Person]])

class EitherSupportTest extends AnyFunSuite {
  val confSchema = Json.schema[People]

  val actual: String = JsonFormatter.format(AsValue.schema(confSchema, Version.Draft04()))

  println(actual)
}

Expected behavior

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "people": {
      "type": "array",
      "items": {
        "type": "object",
        "oneOf": [
          {
            "type": "string"
          },
          {
            "additionalProperties": false,
            "properties": {
              "name": {
                "type": "string"
              },
              "nickname": {
                "type": "string"
              }
            },
            "required": [
              "name",
              "nickname"
            ]
          }
        ]
      }
    }
  },
  "required": [
    "people"
  ]
}

Actual results

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "people": {
      "type": "array",
      "items": {
        "type": "object",
        "oneOf": [
          {
            "additionalProperties": false,
            "properties": {
              "a": {
                "type": "string"
              }
            },
            "required": [
              "a"
            ]
          },
          {
            "additionalProperties": false,
            "properties": {
              "b": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "nickname": {
                    "type": "string"
                  }
                },
                "required": [
                  "name",
                  "nickname"
                ]
              }
            },
            "required": [
              "b"
            ]
          }
        ]
      }
    }
  },
  "required": [
    "people"
  ]
}

Versions:

andyglow commented 3 years ago

there is no common agreement on how to handle either.. i believe different json-libs do it different way. not sure all of them even provide necessary support

so for your case i think the best approach would be to provide custom schema. for that please check joda-time module. there are some examples there