Skip to content

Commit

Permalink
Fix maximum, minimum for Integer (#4335)
Browse files Browse the repository at this point in the history
* fix int/long max, min value (removing decimal)

* fix integer min/max in parameter
  • Loading branch information
wing328 committed Dec 7, 2016
1 parent f781f1d commit 162352c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 44 deletions.
Expand Up @@ -32,15 +32,15 @@ public class CodegenParameter {
/**
* See http://json-schema.org/latest/json-schema-validation.html#anchor17.
*/
public Number maximum;
public String maximum;
/**
* See http://json-schema.org/latest/json-schema-validation.html#anchor17
*/
public Boolean exclusiveMaximum;
/**
* See http://json-schema.org/latest/json-schema-validation.html#anchor21
*/
public Number minimum;
public String minimum;
/**
* See http://json-schema.org/latest/json-schema-validation.html#anchor21
*/
Expand Down
Expand Up @@ -30,8 +30,8 @@ public class CodegenProperty implements Cloneable {
public String example;

public String jsonSchema;
public Double minimum;
public Double maximum;
public String minimum;
public String maximum;
public Boolean exclusiveMinimum;
public Boolean exclusiveMaximum;
public Boolean hasMore, required, secondaryParam;
Expand Down
Expand Up @@ -1444,8 +1444,28 @@ public CodegenProperty fromProperty(String name, Property p) {
String type = getSwaggerType(p);
if (p instanceof AbstractNumericProperty) {
AbstractNumericProperty np = (AbstractNumericProperty) p;
property.minimum = np.getMinimum();
property.maximum = np.getMaximum();
if (np.getMinimum() != null) {
if (p instanceof BaseIntegerProperty) { // int, long
property.minimum = String.valueOf(np.getMinimum().longValue());
} else { // double, decimal
property.minimum = String.valueOf(np.getMinimum());
}
} else {
// set to null (empty) in mustache
property.minimum = null;

This comment has been minimized.

Copy link
@sreeshas

sreeshas Dec 7, 2016

Contributor

nit: By default, minimum is set to null. Explicitly setting it to null is redundant.

This comment has been minimized.

Copy link
@wing328

wing328 Dec 8, 2016

Author Contributor

That's correct. When I've time, I'll switch to the 1-liner if-then-else statement similar to how "parameter" is handled below.

}

if (np.getMaximum() != null) {
if (p instanceof BaseIntegerProperty) { // int, long
property.maximum = String.valueOf(np.getMaximum().longValue());
} else { // double, decimal
property.maximum = String.valueOf(np.getMaximum());
}
} else {
// set to null (empty) in mustache
property.maximum = null;
}

property.exclusiveMinimum = np.getExclusiveMinimum();
property.exclusiveMaximum = np.getExclusiveMaximum();

Expand Down Expand Up @@ -2314,9 +2334,15 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
}

// validation
p.maximum = qp.getMaximum();
// handle maximum, minimum properly for int/long by removing the trailing ".0"
if ("integer".equals(type)) {
p.maximum = qp.getMaximum() == null ? null : String.valueOf(qp.getMaximum().longValue());
p.minimum = qp.getMinimum() == null ? null : String.valueOf(qp.getMinimum().longValue());
} else {
p.maximum = qp.getMaximum() == null ? null : String.valueOf(qp.getMaximum());
p.minimum = qp.getMinimum() == null ? null : String.valueOf(qp.getMinimum());
}
p.exclusiveMaximum = qp.isExclusiveMaximum();
p.minimum = qp.getMinimum();
p.exclusiveMinimum = qp.isExclusiveMinimum();
p.maxLength = qp.getMaxLength();
p.minLength = qp.getMinLength();
Expand Down
Expand Up @@ -553,20 +553,20 @@ public void setParameterExampleValue(CodegenParameter p) {
example = "'" + escapeText(example) + "'";
} else if ("Integer".equals(type) || "int".equals(type)) {
if(p.minimum != null) {
example = "" + (p.minimum.intValue() + 1);
example = "" + (Integer.valueOf(p.minimum) + 1);
}
if(p.maximum != null) {
example = "" + p.maximum.intValue();
example = "" + p.maximum;
} else if (example == null) {
example = "56";
}

} else if ("Long".equalsIgnoreCase(type)) {
if(p.minimum != null) {
example = "" + (p.minimum.longValue() + 1);
example = "" + (Long.valueOf(p.minimum) + 1);
}
if(p.maximum != null) {
example = "" + p.maximum.longValue();
example = "" + p.maximum;
} else if (example == null) {
example = "789";
}
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/ruby/lib/petstore/api/fake_api.rb
Expand Up @@ -145,20 +145,20 @@ def test_endpoint_parameters_with_http_info(number, double, pattern_without_deli

# verify the required parameter 'byte' is set
fail ArgumentError, "Missing the required parameter 'byte' when calling FakeApi.test_endpoint_parameters" if byte.nil?
if !opts[:'integer'].nil? && opts[:'integer'] > 100.0
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 100.0.'
if !opts[:'integer'].nil? && opts[:'integer'] > 100
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 100.'
end

if !opts[:'integer'].nil? && opts[:'integer'] < 10.0
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 10.0.'
if !opts[:'integer'].nil? && opts[:'integer'] < 10
fail ArgumentError, 'invalid value for "opts[:"integer"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 10.'
end

if !opts[:'int32'].nil? && opts[:'int32'] > 200.0
fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 200.0.'
if !opts[:'int32'].nil? && opts[:'int32'] > 200
fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be smaller than or equal to 200.'
end

if !opts[:'int32'].nil? && opts[:'int32'] < 20.0
fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 20.0.'
if !opts[:'int32'].nil? && opts[:'int32'] < 20
fail ArgumentError, 'invalid value for "opts[:"int32"]" when calling FakeApi.test_endpoint_parameters, must be greater than or equal to 20.'
end

if !opts[:'float'].nil? && opts[:'float'] > 987.6
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/ruby/lib/petstore/api/store_api.rb
Expand Up @@ -141,12 +141,12 @@ def get_order_by_id_with_http_info(order_id, opts = {})
end
# verify the required parameter 'order_id' is set
fail ArgumentError, "Missing the required parameter 'order_id' when calling StoreApi.get_order_by_id" if order_id.nil?
if order_id > 5.0
fail ArgumentError, 'invalid value for "order_id" when calling StoreApi.get_order_by_id, must be smaller than or equal to 5.0.'
if order_id > 5
fail ArgumentError, 'invalid value for "order_id" when calling StoreApi.get_order_by_id, must be smaller than or equal to 5.'
end

if order_id < 1.0
fail ArgumentError, 'invalid value for "order_id" when calling StoreApi.get_order_by_id, must be greater than or equal to 1.0.'
if order_id < 1
fail ArgumentError, 'invalid value for "order_id" when calling StoreApi.get_order_by_id, must be greater than or equal to 1.'
end

# resource path
Expand Down
40 changes: 20 additions & 20 deletions samples/client/petstore/ruby/lib/petstore/models/format_test.rb
Expand Up @@ -145,20 +145,20 @@ def initialize(attributes = {})
# @return Array for valid properies with the reasons
def list_invalid_properties
invalid_properties = Array.new
if !@integer.nil? && @integer > 100.0
invalid_properties.push("invalid value for 'integer', must be smaller than or equal to 100.0.")
if !@integer.nil? && @integer > 100
invalid_properties.push("invalid value for 'integer', must be smaller than or equal to 100.")
end

if !@integer.nil? && @integer < 10.0
invalid_properties.push("invalid value for 'integer', must be greater than or equal to 10.0.")
if !@integer.nil? && @integer < 10
invalid_properties.push("invalid value for 'integer', must be greater than or equal to 10.")
end

if !@int32.nil? && @int32 > 200.0
invalid_properties.push("invalid value for 'int32', must be smaller than or equal to 200.0.")
if !@int32.nil? && @int32 > 200
invalid_properties.push("invalid value for 'int32', must be smaller than or equal to 200.")
end

if !@int32.nil? && @int32 < 20.0
invalid_properties.push("invalid value for 'int32', must be greater than or equal to 20.0.")
if !@int32.nil? && @int32 < 20
invalid_properties.push("invalid value for 'int32', must be greater than or equal to 20.")
end

if @number.nil?
Expand Down Expand Up @@ -219,10 +219,10 @@ def list_invalid_properties
# Check to see if the all the properties in the model are valid
# @return true if the model is valid
def valid?
return false if !@integer.nil? && @integer > 100.0
return false if !@integer.nil? && @integer < 10.0
return false if !@int32.nil? && @int32 > 200.0
return false if !@int32.nil? && @int32 < 20.0
return false if !@integer.nil? && @integer > 100
return false if !@integer.nil? && @integer < 10
return false if !@int32.nil? && @int32 > 200
return false if !@int32.nil? && @int32 < 20
return false if @number.nil?
return false if @number > 543.2
return false if @number < 32.1
Expand All @@ -243,12 +243,12 @@ def valid?
# @param [Object] integer Value to be assigned
def integer=(integer)

if !integer.nil? && integer > 100.0
fail ArgumentError, "invalid value for 'integer', must be smaller than or equal to 100.0."
if !integer.nil? && integer > 100
fail ArgumentError, "invalid value for 'integer', must be smaller than or equal to 100."
end

if !integer.nil? && integer < 10.0
fail ArgumentError, "invalid value for 'integer', must be greater than or equal to 10.0."
if !integer.nil? && integer < 10
fail ArgumentError, "invalid value for 'integer', must be greater than or equal to 10."
end

@integer = integer
Expand All @@ -258,12 +258,12 @@ def integer=(integer)
# @param [Object] int32 Value to be assigned
def int32=(int32)

if !int32.nil? && int32 > 200.0
fail ArgumentError, "invalid value for 'int32', must be smaller than or equal to 200.0."
if !int32.nil? && int32 > 200
fail ArgumentError, "invalid value for 'int32', must be smaller than or equal to 200."
end

if !int32.nil? && int32 < 20.0
fail ArgumentError, "invalid value for 'int32', must be greater than or equal to 20.0."
if !int32.nil? && int32 < 20
fail ArgumentError, "invalid value for 'int32', must be greater than or equal to 20."
end

@int32 = int32
Expand Down

0 comments on commit 162352c

Please sign in to comment.