Skip to content

Commit

Permalink
Set format to int64 based on the min and max values provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangababu-R committed Sep 1, 2022
1 parent 3991ef8 commit 13ab9d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
22 changes: 14 additions & 8 deletions openapiart/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,11 +1355,13 @@ def _get_openapi_types(self, yobject):
if "type" in yproperty and yproperty["type"] == "array":
class_name += "Iter"
pt.update({"type": "'%s'" % class_name})
sub_properties = yproperty
if (
len(ref) == 0
and "items" in yproperty
and "type" in yproperty["items"]
):
sub_properties = yproperty["items"]
pt.update(
{"itemtype": self._get_data_types(yproperty["items"])}
)
Expand All @@ -1370,14 +1372,18 @@ def _get_openapi_types(self, yobject):
% yproperty["items"]["format"]
}
)
if len(ref) == 0 and "minimum" in yproperty:
pt.update({"minimum": yproperty["minimum"]})
if len(ref) == 0 and "maximum" in yproperty:
pt.update({"maximum": yproperty["maximum"]})
if len(ref) == 0 and "minLength" in yproperty:
pt.update({"minLength": yproperty["minLength"]})
if len(ref) == 0 and "maxLength" in yproperty:
pt.update({"maxLength": yproperty["maxLength"]})
min_max = sub_properties.get("maximum", sub_properties.get("minimum", 0))
key = "itemformat" if pt.get("itemtype") is not None else "format"
if min_max > 2147483647 and pt.get("itemtype") is None:
pt.update({key: r"'int64'"})
if len(ref) == 0 and "minimum" in sub_properties:
pt.update({"minimum": sub_properties["minimum"]})
if len(ref) == 0 and "maximum" in sub_properties:
pt.update({"maximum": sub_properties["maximum"]})
if len(ref) == 0 and "minLength" in sub_properties:
pt.update({"minLength": sub_properties["minLength"]})
if len(ref) == 0 and "maxLength" in sub_properties:
pt.update({"maxLength": sub_properties["maxLength"]})
if len(pt) > 0:
types.append((name, pt))

Expand Down
5 changes: 4 additions & 1 deletion openapiart/tests/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ components:
x-field-uid: 4
full_duplex_100_mb:
type: integer
minimum: 0
maximum: 4261412864
x-field-uid: 5
response:
description: |-
Expand Down Expand Up @@ -159,7 +161,8 @@ components:
type: array
items:
type: integer
format: int64
minimum: 0
maximum: 4261412864
x-field-uid: 31
header_checksum:
x-field-pattern:
Expand Down
24 changes: 24 additions & 0 deletions openapiart/tests/test_int64.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import importlib

module = importlib.import_module("sanity")

def test_int64(default_config):
value1 = default_config._TYPES.get("full_duplex_100_mb")
value2 = default_config._TYPES.get("integer64_list")
assert value1.get("format") is not None
assert value2.get("itemformat") is not None
assert value1.get("format") == "int64"
assert value2.get("itemformat") == "int64"
default_config.full_duplex_100_mb = 100
default_config.integer64_list = [2000]
data = default_config.serialize("dict")
assert isinstance(data["full_duplex_100_mb"], str)
assert isinstance(data["integer64_list"][0], str)

config = module.Api().prefix_config()
config.deserialize(data)
assert isinstance(config.full_duplex_100_mb, int)
assert isinstance(config.integer64_list[0], int)



0 comments on commit 13ab9d4

Please sign in to comment.