CRDGenerator: Cannot specify @Pattern
annotation on type parameter #6282
Description
Is your enhancement related to a problem? Please describe
Consider the following field in a CRD class:
@JsonPropertyDescription("A list of names matching the pattern `[A-Za-z][A-Za-z0-9_]*`")
@JsonProperty
public List<String> names;
What I would like to be able to add the @io.fabric8.generator.annotation.Pattern
annotation so that the Kubernetes API server can validate that the elements of the array match the pattern.
Adding the annotation to the field itself causes the following JSONSchema to be created, which is not what I want:
names:
description: A list of names matching the pattern `[A-Za-z][A-Za-z0-9_]*`
items:
type: string
type: array
pattern: [A-Za-z][A-Za-z0-9_]*
Describe the solution you'd like
I'd like to declare the validation constraint as follows:
@JsonPropertyDescription("A list of names")
@JsonProperty
public List<@Pattern("[A-Za-z][A-Za-z0-9_]*") String> names;
And have that generate the following JSONSchema:
names:
description: A list of names`
items:
type: string
pattern: [A-Za-z][A-Za-z0-9_]*
type: array
This can be achieved by changing the @Target
on @Pattern
to include ElementType.TYPE_USE
, and detecting the presence of the annotation on the type parameters in the declaration to include the pattern
constraint.
Describe alternatives you've considered
I've tried various ways to "trick" Fabric8 into letting me put the pattern
constraint in the correct place, and nothing has worked.
I tried creating a wrapper class for the string that I can attach the annotation to:
@JsonSerialize(as = String.class)
public class Name {
private final String value;
@JsonCreator
public Name(String value) {
this.value = value;
}
@Pattern("[A-Za-z][A-Za-z0-9_]*")
@JsonValue
public String getValue() {
return value;
}
}
Unfortunately, the CRD generator does not detect that this class is supposed to be serialized as a string.
The other alternative would be to just have a normal wrapper object that I can attach the annotation to, but that would create gratuitous nesting of properties that I do not want.
Additional context
No response