Skip to content

@Schema annotation on custom string type is ignored when using @JsonValue #4847

Open
@GeorgEchterling

Description

@GeorgEchterling

Related: #3904

I have these classes:

@Schema(name = "Color", type = "string", pattern = Color.REGEXP, description = "A CSS color hex code.")
public record Color(
    @JsonValue
    @Pattern(regexp = REGEXP) @NonNull String hex
) {
    public static final String REGEXP = "^#[0-9a-f]{6}$";

    @JsonCreator
    public static @NonNull Color deserialize(@NonNull String hex) {
        return new Color(hex);
    }
}

@Builder
@Jacksonized
public record Colors(
    @Valid Color font,
    @Valid Color background,
    @Valid Color listpoint
) {
}

I would expect fields using the Color class to be resolved to a Color type like this:

{
    "Colors": {
      "type": "object",
      "properties": {
        "font": { "$ref": "#/definitions/Color" },
        "background": { "$ref": "#/definitions/Color" },
        "listpoint": { "$ref": "#/definitions/Color" }
      }
    },
    "Color": {
      "type": "string",
      "pattern": "^#[0-9a-f]{6}$"
    }
}

Instead, they get resolved to a simple string type without the configured pattern:

{
  "Colors": {
    "type": "object",
    "properties": {
      "font": { "type": "string" },
      "background": { "type": "string" },
      "listpoint": { "type": "string" }
    }
  }
}

I have tried annotating the field Color.hex, but this also gets ignored. As far as I can tell, there is currently no way to have named types extend string.

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