Skip to content

Commit 7d86061

Browse files
gustavodemoraisMec-iS
authored andcommitted
Added support for OAS Data Types (#15)
* Update README.md * Added support for OAS Data types * Updated sample output
1 parent e80f75f commit 7d86061

3 files changed

Lines changed: 768 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# hydra-openapi-parser
2-
This library contains the OpenAPI parser implemntaion in Python to be used with hydrus and python-hydra-agent.
2+
This library contains the OpenAPI parser implemntaion in Python to be used with `hydrus` and `hydra-python-agent`.
33

44
Currently the library consists of openapi_parser module which helps hydrus parse OpenAPI standard docs.
55

hydra_openapi_parser/openapi_parser.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,35 @@ def allow_parameter(parameter: Dict[str, Any]) -> bool:
356356
# can add rules about param processing
357357
# param can be in path too , that is already handled when we declared
358358
# the class as collection from the endpoint
359-
params_location = ["body"]
360-
if parameter["in"] not in params_location:
361-
return False
362-
return True
359+
params_location = ["body", "integer", "string", "long", "float",\
360+
"boolean", "dateTime", "Date", "array"]
361+
try:
362+
if parameter["type"] in params_location:
363+
return True
364+
except KeyError:
365+
if parameter["in"] in params_location:
366+
return True
367+
368+
return False
369+
370+
371+
def type_ref_mapping(type: str)->str:
372+
"""
373+
Returns semantic ref for OAS data types
374+
:param type: data type
375+
:return: ref
376+
"""
377+
dataType_ref_map = dict()
378+
# todo add support for byte , binary , password ,double data types
379+
dataType_ref_map["integer"] = "https://schema.org/Integer"
380+
dataType_ref_map["string"] = "https://schema.org/Text"
381+
dataType_ref_map["long"] = "http://books.xmlschemata.org/relaxng/ch19-77199.html"
382+
dataType_ref_map["float"] = "https://schema.org/Float"
383+
dataType_ref_map["boolean"] = "https://schema.org/Boolean"
384+
dataType_ref_map["dateTime"] = "https://schema.org/DateTime"
385+
dataType_ref_map["date"] = "https://schema.org/Date"
386+
387+
return dataType_ref_map[type]
363388

364389

365390
def get_parameters(global_: Dict[str, Any],
@@ -396,7 +421,25 @@ def get_parameters(global_: Dict[str, Any],
396421
param = "vocab:{}".format(
397422
parameter["schema"]["$ref"].split('/')[2])
398423
except KeyError:
399-
param = ""
424+
type = parameter["type"]
425+
if type == "array":
426+
# TODO adaptation to array representation after discussion
427+
items = parameter["items"]
428+
try:
429+
if items["$ref"].split(
430+
'/')[2] in global_["class_names"]:
431+
param = "vocab" + items["$ref"].split('/')[2]
432+
else:
433+
get_class_details(
434+
global_, get_data_at_location(
435+
items["$ref"]), items["$ref"].split('/')[2], path=path)
436+
param = "vocab" + items["$ref"].split('/')[2]
437+
except KeyError:
438+
param = type_ref_mapping(items["type"])
439+
elif type == "object":
440+
param = "string"
441+
else:
442+
param = type_ref_mapping(type)
400443

401444
return param
402445

0 commit comments

Comments
 (0)