andyglow / scala-jsonschema

Scala JSON Schema
Other
122 stars 38 forks source link

Missing "definitions" literal in $ref regular mode schemas #224

Closed mpindado closed 2 years ago

mpindado commented 2 years ago

Describe the bug When printing the schema generated, $ref definitions do not contain the literal "definitions".

    "cars": {
      "type": "array",
      "items": {
        "$ref": "#Car"   => shouldn't it be #definitions/Car?
      }
    },

The json schemas generated are not correctly parsed by some third party tools.

The documentation in the README shows the examples containing #definitions literal.

Is this a bug or can be configured somehow?

To Reproduce The example in the README:

import com.github.andyglow.json.JsonFormatter
import com.github.andyglow.jsonschema.AsValue
import json._
import json.schema.Version.Draft07

sealed trait Gender

object Gender {
  case object Male extends Gender
  case object Female extends Gender
}

case class Company(name: String)
case class Car(name: String, manufacturer: Company)
case class Person(
                   firstName: String,
                   middleName: Option[String],
                   lastName: String,
                   gender: Gender,
                   birthDay: java.time.LocalDateTime,
                   company: Company,
                   cars: Seq[Car])

implicit val genderSchema: json.Schema[Gender] = Json.schema[Gender]
implicit val companySchema: json.Schema[Company] = Json.schema[Company]
implicit val carSchema: json.Schema[Car] = Json.schema[Car]
implicit val personSchema: json.Schema[Person] = Json.schema[Person]

JsonFormatter.format(AsValue.schema(personSchema, Draft07("Person")))

Expected behavior References should contain "definitions" literal.

Actual results References do not contain "definitions" literal.

Versions:

andyglow commented 2 years ago

as per chapter 9.2.1. Internal References of the spec https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-00#section-9.2.1

since draft07 dereference is done by $id and not by path

mpindado commented 2 years ago

So it is a bug of the third party library then.

Thank you so much

andyglow commented 2 years ago

sure, no problem. @mpindado you can try draft04 btw if you need this behavior