swagger-api / swagger-api/swagger-core

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

Open
#4,847 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backlog
Dominant language
Java
Stars
7.5k
Forks
2.3k
Avg merge
18h 1m
Merged PRs (30d)
10

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing schema generation with the Color and Colors records shown in the issue, using @Schema, @JsonValue, @JsonCreator, and @Valid. Trace how the named Color type is resolved when it is used as a field, then verify that the generated schema preserves the Color reference and configured pattern instead of reducing it to an unannotated string.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.