Skip to content

Commit

Permalink
fix fix nlohmann#18
Browse files Browse the repository at this point in the history
  • Loading branch information
TOTOleHero committed Dec 20, 2020
1 parent 45a9e08 commit d54d9c0
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions swagger_to_uml.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def __init__(self, name, type, required, example=None, description=None, default
# type
self.type = type # type: str
self.format = format # type: Optional[str]
self.ref_type = ref_type # type: Optional[str]
self.ref_type = ref_type # type: Optional[Any] List[str] or str


# constraints
self.required = required # type: bool
Expand Down Expand Up @@ -63,7 +64,7 @@ def __init__(self, name, type, required, example=None, description=None, default
self.max_items = max_items # type: Optional[int]
self.min_items = min_items # type: int
self.unique_items = unique_items # type: bool
self.items = items # type: Optional[str]
self.items = items # type: Optional[List[str]]

@staticmethod
def from_dict(property_name, d, required):
Expand Down Expand Up @@ -98,11 +99,20 @@ def from_dict(property_name, d, required):
if 'items' in type_dict:
if 'type' in type_dict['items']:
items = type_dict['items']['type']

elif 'allOf' in type_dict['items']:
items = []
for ref in type_dict['items']['allOf']:
items.append(resolve_ref(ref.get('$ref')))
ref_type = items
else:
items = resolve_ref(type_dict['items']['$ref'])
ref_type = items
else:
items = None




return Property(
name=property_name,
Expand Down Expand Up @@ -180,11 +190,12 @@ def uml(self):


class Definition:
def __init__(self, name, type, properties, relationships):
def __init__(self, name, type, properties, relationships,allOf):
self.name = name # type: str
self.type = type # type: str
self.properties = properties # type: List[Property]
self.relationships = relationships # type: Set[str]
self.allOf = allOf

@staticmethod
def from_dict(name, d):
Expand All @@ -199,10 +210,26 @@ def from_dict(name, d):
if not 'type' in d:
print('required key "type" not found in dictionary ' + json.dumps(d), file=sys.stderr)

allOf = []
if 'allOf' in d:
for ref in d.get('allOf', []):
allOf.append(resolve_ref(ref.get('$ref')))


relationships=[]
for property in properties :
if property.ref_type:
if type(property.ref_type) is list:
for ref in property.ref_type:
relationships.append(ref)
else:
relationships.append(property.ref_type)

return Definition(name=name,
type=d['type'],
properties=properties,
relationships={property.ref_type for property in properties if property.ref_type})
relationships=relationships,
allOf=allOf)

@property
def uml(self):
Expand All @@ -218,6 +245,9 @@ def uml(self):
for relationship in sorted(self.relationships):
result += '{name} ..> {relationship}\n'.format(name=self.name, relationship=relationship)

for ref in sorted(self.allOf):
result += '{ref} <|-- {name}\n'.format(ref=ref,name=self.name)

return result


Expand Down

0 comments on commit d54d9c0

Please sign in to comment.