Skip to content
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

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

Open
GeorgEchterling opened this issue Feb 25, 2025 · 0 comments

Comments

@GeorgEchterling
Copy link

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.

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

No branches or pull requests

1 participant