Skip to content

Introduce format annotation to control string-types in openapi spec #3489

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jdsalchow
Copy link

Add support for string format annotations

Issue

Fixes #3488

Description

This PR introduces a new format annotation that can be used to specify the format of string properties in OpenAPI schemas. This allows for automatic handling of string-based object properties according to the OpenAPI specification's format property, as defined in JSON Schema Validation.

Implementation Details

  • Added a format annotation that allows overriding the default format in the schema
  • Don't ignoring validation annotations for string types other than string
  • Set default StringFormat for various format types:
    • LocalDate -> date
    • Instant | OffsetDateTime | ZonedDateTime -> date-time
    • OffsetTime -> time
    • Period | Duration -> Duration
    • UUID -> UUID

Example Usage

final case class EmailData(
  @format(Email) email: String, 
  received: Option[java.time.LocalDate]
) derives Schema

This generates an OpenAPI schema with the appropriate format annotations:

{
  "email": {
    "type": "string",
    "format": "email"
  },
  "received": {
    "type": ["string", "null"],
    "format": "date"
  }
}

annotations.collect { case zio.schema.annotation.validate(v) => v }.headOption,
annotations.collect { case format(stringFormat) => stringFormat }.headOption,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think an annotation is the right solution.
Especially not one that lives in this file.

I think right solution is to have a mapping of StandardType to a format.
If this is not precise enough, then we need something in zio schema to make it configurable

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

Successfully merging this pull request may close these issues.

Allow setting format and validations in open-api spec for all properties of type string
2 participants