Skip to content

[Feature Request] codegen: do not wrap types in Option if a default is present #5492

Description

@Iilun

Hello maintainers.

This one is more of a nice to have/ improvement, and there may be a reason it was implemented this way but I am unsure why.

Currently, when generating the schema for the following object for example:

title: MyBook
properties:
  title:
    type: string
  genre:
    type: string
    default: unknown
required:
 - title

The generated case class is:

case class Book (
  title: String,
  genre: Option[String] = Some("unknown")
)

Here the genre field is marked as option, but in reality it is always set to a Some even if it is absent from the request. Handling the option later then requires to .get it, or .getOrElse with a default value, but it is already defined in the class itself !

My idea is to introduce a new parameter for generation (to avoid breaking existing generations, as an opt in behavior), named generateDefaultsAsRequired or something alike.

When this parameter is set, the above yaml would result in the following scala code:

case class Book (
  title: String,
  genre: String = "unknown"
)

The logic behind it is:

  • if param is required -> no option wrapping
  • if param has a default and is not nullable -> no option wrapping
  • all other cases -> option wrapping

I have already drafted working code on this so maybe you can have a better idea of what it could look like.

For circe, this does need to add an external dependency (which is experimental) to allow for default values to be absent from the body, hence why this is an opt-in behavior, but for the other serdes it works out of the box.

One last difference between the serdes to note is if the request contains an explicit null on a nullable field:

  • circe correctly interprets it as a None instead of the default
  • both others interpret it as the default instead

However this is not a behavior exclusive to this proposed change, and is instead the current behavior for all nullable fields if I am not mistaken.

Thank you for your time !

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions