Skip to content

Commit 64893b5

Browse files
committed
fix: add support for decimal min and max #326
Signed-off-by: anessi <[email protected]>
1 parent 4e2766b commit 64893b5

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"required": false
120120
}
121121
},
122-
"generator": ">=1.8.27 <2.0.0",
122+
"generator": ">=1.8.27",
123123
"filters": [
124124
"@asyncapi/generator-filters"
125125
],

template/src/main/java/com/asyncapi/model/$$objectSchema$$.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,13 @@ public class {{allName}} {
147147
{%- if prop.deprecated() %}@Deprecated{% endif %}
148148
{%- if prop.minLength() or prop.maxLength() or prop.maxItems() or prop.minItems() %}@Size({% if prop.minLength() or prop.minItems() %}min = {{prop.minLength()}}{{prop.minItems()}}{% endif %}{% if prop.maxLength() or prop.maxItems() %}{% if prop.minLength() or prop.minItems() %},{% endif %}max = {{prop.maxLength()}}{{prop.maxItems()}}{% endif %}){% endif %}
149149
{%- if prop.pattern() %}@Pattern(regexp="{{prop.pattern() | addBackSlashToPattern}}"){% endif %}
150-
{%- if prop.minimum() %}@Min({{prop.minimum()}}){% endif %}{% if prop.exclusiveMinimum() %}@Min({{prop.exclusiveMinimum() + 1}}){% endif %}
151-
{%- if prop.maximum() %}@Max({{prop.maximum()}}){% endif %}{% if prop.exclusiveMaximum() %}@Max({{prop.exclusiveMaximum() + 1}}){% endif %}
150+
{%- if prop.type() == 'number' and (prop.format() == 'float' or prop.format() == 'double') %}
151+
{%- if prop.minimum() %}@DecimalMin(value = "{{prop.minimum()}}", inclusive = true){% endif %}{%- if prop.exclusiveMinimum() %}@DecimalMin(value = "{{prop.exclusiveMinimum()}}", inclusive = false){% endif %}
152+
{%- if prop.maximum() %}@DecimalMax(value = "{{prop.maximum()}}", inclusive = true){% endif %}{%- if prop.exclusiveMaximum() %}@DecimalMax(value = "{{prop.exclusiveMaximum()}}", inclusive = false){% endif %}
153+
{%- else %}
154+
{%- if prop.minimum() %}@Min({{prop.minimum()}}){% endif %}{% if prop.exclusiveMinimum() %}@Min({{prop.exclusiveMinimum() + 1}}){% endif %}
155+
{%- if prop.maximum() %}@Max({{prop.maximum()}}){% endif %}{% if prop.exclusiveMaximum() %}@Max({{prop.exclusiveMaximum() + 1}}){% endif %}
156+
{%- endif %}
152157
public {{propType}} get{{className}}() {
153158
return {{varName}};
154159
}
@@ -202,4 +207,4 @@ private String toIndentedString(Object o) {
202207
}
203208
return o.toString().replace("\n", "\n ");
204209
}
205-
}
210+
}

tests/__snapshots__/additional-formats.test.js.snap

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public class SongPayload {
3232
3333
private @Valid Integer stars;
3434
35+
private @Valid Float length;
36+
37+
private @Valid Double playtime;
38+
3539
3640
3741
@@ -113,6 +117,32 @@ public class SongPayload {
113117
this.stars = stars;
114118
}
115119
120+
121+
/**
122+
* Length of the song in minutes
123+
*/
124+
@JsonProperty("length")@DecimalMin(value = "0.5", inclusive = true)@DecimalMax(value = "10.5", inclusive = true)
125+
public Float getLength() {
126+
return length;
127+
}
128+
129+
public void setLength(Float length) {
130+
this.length = length;
131+
}
132+
133+
134+
/**
135+
* Playtime of the song in minutes
136+
*/
137+
@JsonProperty("playtime")@DecimalMin(value = "1.5", inclusive = false)@DecimalMax(value = "11.5", inclusive = false)
138+
public Double getPlaytime() {
139+
return playtime;
140+
}
141+
142+
public void setPlaytime(Double playtime) {
143+
this.playtime = playtime;
144+
}
145+
116146
@Override
117147
public boolean equals(Object o) {
118148
if (this == o) {
@@ -128,12 +158,14 @@ public class SongPayload {
128158
Objects.equals(this.uri, songPayload.uri) &&
129159
Objects.equals(this.email, songPayload.email) &&
130160
Objects.equals(this.rating, songPayload.rating) &&
131-
Objects.equals(this.stars, songPayload.stars);
161+
Objects.equals(this.stars, songPayload.stars) &&
162+
Objects.equals(this.length, songPayload.length) &&
163+
Objects.equals(this.playtime, songPayload.playtime);
132164
}
133165
134166
@Override
135167
public int hashCode() {
136-
return Objects.hash(id, title, uri, email, rating, stars);
168+
return Objects.hash(id, title, uri, email, rating, stars, length, playtime);
137169
}
138170
139171
@Override
@@ -146,6 +178,8 @@ public class SongPayload {
146178
" email: " + toIndentedString(email) + "\\n" +
147179
" rating: " + toIndentedString(rating) + "\\n" +
148180
" stars: " + toIndentedString(stars) + "\\n" +
181+
" length: " + toIndentedString(length) + "\\n" +
182+
" playtime: " + toIndentedString(playtime) + "\\n" +
149183
"}";
150184
}
151185

tests/mocks/additional-type-formats.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@ components:
4545
stars:
4646
description: "Number of stars. Deprecated: Use rating"
4747
type: integer
48-
deprecated: true
48+
deprecated: true
49+
length:
50+
description: Length of the song in minutes
51+
type: number
52+
format: float
53+
minimum: 0.5
54+
maximum: 10.5
55+
playtime:
56+
description: Playtime of the song in minutes
57+
type: number
58+
format: double
59+
exclusiveMinimum: 1.5
60+
exclusiveMaximum: 11.5

0 commit comments

Comments
 (0)