quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

[BUG]: Generating Scala3 uPickle from JSON Schema is missing ReadWriter annotation for enums

Open Bathtor opened this issue 9 months ago • 0 comments

Issue Type

Output

Context (Environment, Version, Language)

Input Format: JSON Schema Output Language: Scala3+uPickle

CLI, npm, or app.quicktype.io: CLI Version: 23.0.170

Description

In the generated Scala3 output there is a missing ReadWriter annotation for the generated enum Color, which leads to a build failure while the uPickle macro is trying run:

No given instance of type ReadersVersionSpecific_this.Reader[
[error]     | quicktype.Color] was found.

Input Data

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "definitions": {
    "Color" : {
      "type" : "string",
      "enum": ["Red", "Green", "Blue"]
    },
    "Monster" : {
      "type" : "object",
      "properties" : {
        "name" : {
                "type" : "string"
              },
        "color" : {
                "$ref" : "#/definitions/Color"
              }
      },
      "required" : ["name", "color"],
      "additionalProperties" : false
    }
  },
  "$ref" : "#/definitions/Monster"
}

Expected Behaviour / Output

package quicktype

import upickle.default.*

case class Monster (
    val color : Color,
    val name : String
) derives ReadWriter 

enum Color derives ReadWriter :  
	 case Blue,Green,Red

Current Behaviour / Output

package quicktype

import upickle.default.*

case class Monster (
    val color : Color,
    val name : String
) derives ReadWriter 

enum Color :  
	 case Blue,Green,Red

Steps to Reproduce

It also reproduces in the webapp, so easiest repro is:

  1. paste above json into webapp and JSON Schema as source
  2. Select Scala3 and uPickle as target
  3. Try to compile the resulting code with with a minimal sbt project. Something along the lines of:
val scala3Version = "3.6.2"

lazy val root = project
  .in(file("."))
  .settings(
    name := "Quicktype Test",
    version := "0.1.0-SNAPSHOT",
    
    scalaVersion := scala3Version,
    libraryDependencies += "com.lihaoyi" %%% "upickle" % "4.1.0"
  )

Possible Solution

Just adjust the generator to add the necessary derive for enums.

Bathtor avatar Jan 20 '25 08:01 Bathtor