swagger-api / swagger-api/swagger-core
@Schema annotation on custom string type is ignored when using @JsonValue
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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