Issue
I'm using swagger 2 and open api 5.3.1 generator to generate some classes. Currently, it does not seem to be generating a pattern annotation on Long(defined as int64 in swagger). How can I do this?
swagger:
id:
type: integer
format: int64
pattern: '^\d{16,19}$'
minLength: 16
maxLength: 19
code generated
@javax.annotation.Nonnull
@NotNull
@ApiModelProperty( required = true, )
@JsonProperty(JSON_PROPERTY_ID)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public Long getId() {
return id;
}
Solution
The pattern keyword lets you define a regular expression template for the string value
It does not support Integer/Long
https://swagger.io/docs/specification/data-models/data-types/#pattern
The documentation states:
The pattern keyword lets you define a regular expression template for the string value. Only the values that match this template will be accepted. The regular expression syntax used is from JavaScript (more specifically, ECMA 262). Regular expressions are case-sensitive, that is, [a-z] and [A-Z] are different expressions. For example, the following pattern matches a Social Security Number (SSN) in the 123-45-6789 format:
ssn:
type: string
pattern: '^\d{3}-\d{2}-\d{4}$'
Note that the regular expression is enclosed in the ^…$ tokens, where ^ means the beginning of the string, and $ means the end of the string. Without ^…$, pattern works as a partial match, that is, matches any string that contains the specified regular expression. For example, pattern: pet matches pet, petstore and carpet. The ^…$ token forces an exact match.
For the datatype integer, it will look like:
type: integer
format: int64
minimum: -86400
maximum: 86400
To clarify minimum/maximum:
Minimum and Maximum Use the minimum and maximum keywords to specify the range of possible values:
type: integer
minimum: 1
maximum: 20
By default, the minimum and maximum values are included in the range, that is:
minimum ≤ value ≤ maximum
To exclude the boundary values, specify exclusiveMinimum: true and exclusiveMaximum: true. For example, you can define a floating-point number range as 0–50 and exclude the 0 value:
type: number
minimum: 0
exclusiveMinimum: true
maximum: 50
The word “exclusive” in exclusiveMinimum and exclusiveMaximum means the corresponding boundary is excluded: Keyword Description exclusiveMinimum: false or not included value ≥ minimum exclusiveMinimum: true value > minimum exclusiveMaximum: false or not included value ≤ maximum exclusiveMaximum: true value < maximum
Answered By - SMA
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.