Skip to content

Commit

Permalink
v0.2.16
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutshkumr committed Mar 27, 2023
1 parent 5cd21ec commit 82e01e6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 81 deletions.
1 change: 1 addition & 0 deletions openapiart/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,7 @@ def _get_data_types(self, yproperty):

def _get_openapi_types(self, yobject):
types = []
# TODO: revisit this portion for data type refactor
if "properties" in yobject:
for name in yobject["properties"]:
yproperty = yobject["properties"][name]
Expand Down
14 changes: 9 additions & 5 deletions openapiart/openapiartgo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .openapiartplugin import OpenApiArtPlugin
from .openapiartplugin import OpenApiArtPlugin, type_limits
import os
import subprocess

Expand Down Expand Up @@ -2406,10 +2406,14 @@ def _validate_types(self, new, field):
inner_body = ""
if field.hasminmax and ("int" in field.type or "float" in field.type):
line = []
if field.min is None and "int" in field.type:
field.min = -(2**31 if "32" in field.type else 2**63)
if field.max is None and "int" in field.type:
field.max = (2**31 if "32" in field.type else 2**63) - 1
if "int" in field.type:
type_min, type_max = type_limits.limits.get(
field.type, (None, None)
)
if field.min is None and type_min is not None:
field.min = type_min
if field.max is None and type_max is not None:
field.max = type_max
if field.min is not None:
line.append("{pointer}{value} < {min}")
if field.max is not None:
Expand Down
77 changes: 77 additions & 0 deletions openapiart/openapiartplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,80 @@ def _justify_desc(self, text, indent=0, use_multi=False):
+ " */"
)
return "{}// ".format(indent) + "\n{}// ".format(indent).join(lines)


class type_limits(object):

limits = {
"int32": (-2147483648, 2147483647),
"uint32": (0, 4294967295),
"int64": (-9223372036854775808, 9223372036854775807),
"uint64": (-0, 18446744073709551615),
}

@classmethod
def _min_max_in_range(cls, format, min, max):
if min is None or max is None:
return True
elif min is None or max is None:
if min is None:
min = max
if max is None:
max = min

if min > max:
return False

if format in cls.limits:
type_min, type_max = cls.limits[format]
return min >= type_min and max <= type_max
else:
raise Exception("unsupported integer format: {}".format(format))

@classmethod
def _format_from_range(cls, min, max):
if min is None and max is None:
return "int32"
elif min is None or max is None:
if min is None:
min = max
if max is None:
max = min

if max < min:
raise Exception("min %d cannot be less than max %d", min, max)

# TODO: this logic needs to be replaced with the one commented out below
if min > 2147483647 or max > 2147483647:
return "int64"
else:
return "int32"
# TODO: following snippet is more accurate but introduces breaking
# changes and hence commented out
# if self._min_max_in_range("uint32", min, max):
# return "uint32"
# if self._min_max_in_range("uint64", min, max):
# return "uint64"
# if self._min_max_in_range("int32", min, max):
# return "int32"
# if self._min_max_in_range("int64", min, max):
# return "int64"

@classmethod
def _get_integer_format(cls, type_format, min, max):
valid_formats = ["int32", "int64", "uint32", "uint64"]
if type_format is not None:
if type_format in valid_formats:
if not cls._min_max_in_range(type_format, min, max):
raise Exception(
"format {} is not compatible with [min,max] [{},{}]".format(
type_format, min, max
)
)
return type_format
raise Exception(
"unsupported format %s, supported formats are: %s",
type_format,
valid_formats,
)
return cls._format_from_range(min, max)
80 changes: 5 additions & 75 deletions openapiart/openapiartprotobuf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import subprocess
import os
from .openapiartplugin import OpenApiArtPlugin
from .openapiartplugin import OpenApiArtPlugin, type_limits


class OpenApiArtProtobuf(OpenApiArtPlugin):
Expand Down Expand Up @@ -262,7 +262,7 @@ def _get_field_type(
max = outer_max
if outer_format is not None:
type_format = outer_format
return _get_integer_format(type_format, min, max)
return type_limits._get_integer_format(type_format, min, max)
if type == "number":
if "format" in openapi_object:
if (
Expand Down Expand Up @@ -291,7 +291,9 @@ def _get_field_type(
)

if item_type == "integer":
item_type = _get_integer_format(type_format, min, max)
item_type = type_limits._get_integer_format(
type_format, min, max
)
return "repeated " + item_type
elif "$ref" in openapi_object:
return openapi_object["$ref"].split("/")[-1].replace(".", "")
Expand Down Expand Up @@ -446,75 +448,3 @@ def _write_rpc(self, url, method, path_item_object):
operation.rpc, operation.request, "", operation.response
)
self._write(line, indent=1)


def _min_max_in_range(format, min, max):
if min is None or max is None:
return True
elif min is None or max is None:
if min is None:
min = max
if max is None:
max = min

if min > max:
return False

if format == "uint32":
return min >= 0 and max <= 4294967295
elif format == "uint64":
return min >= 0 and max <= 18446744073709551615
elif format == "int32":
return min >= -2147483648 and max <= 2147483647
elif format == "int64":
return min >= -9223372036854775808 and max <= 9223372036854775807
else:
raise Exception("unsupported integer format: {}".format(format))


def _format_from_range(min, max):
if min is None and max is None:
return "int32"
elif min is None or max is None:
if min is None:
min = max
if max is None:
max = min

if max < min:
raise Exception("min %d cannot be less than max %d", min, max)

# TODO: this logic needs to be replaced with the one commented out below
if min > 2147483647 or max > 2147483647:
return "int64"
else:
return "int32"
# TODO: following snippet is more accurate but introduces breaking
# changes and hence commented out
# if self._min_max_in_range("uint32", min, max):
# return "uint32"
# if self._min_max_in_range("uint64", min, max):
# return "uint64"
# if self._min_max_in_range("int32", min, max):
# return "int32"
# if self._min_max_in_range("int64", min, max):
# return "int64"


def _get_integer_format(type_format, min, max):
valid_formats = ["int32", "int64", "uint32", "uint64"]
if type_format is not None:
if type_format in valid_formats:
if not _min_max_in_range(type_format, min, max):
raise Exception(
"format {} is not compatible with [min,max] [{},{}]".format(
type_format, min, max
)
)
return type_format
raise Exception(
"unsupported format %s, supported formats are: %s",
type_format,
valid_formats,
)
return _format_from_range(min, max)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import setuptools

pkg_name = "openapiart"
version = "0.2.15"
version = "0.2.16"

base_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(base_dir, "README.md")) as fid:
Expand Down

0 comments on commit 82e01e6

Please sign in to comment.