Skip to content

Commit

Permalink
Update swagger generator to reference named schemas instead of inlini…
Browse files Browse the repository at this point in the history
…ng them. Fixes #116
  • Loading branch information
stuartpullinger committed Nov 22, 2019
1 parent 274c0c1 commit 762f490
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/swagger/swagger_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def __init__(self, entity_name, model):
self.properties_dict = {}
for column in model.__table__.columns:
self.properties_dict[column.name] = {"type": self._map_db_type_to_json_type(str(column.type))}
self.entity_schema = {f"{SwaggerGenerator.singularise(entity_name)}": {
"type": "object",
"properties": self.properties_dict}}

self.entity_no_id_endpoint = {
f"/{entity_name.lower()}": {
"get": {
Expand All @@ -81,8 +85,7 @@ def __init__(self, entity_name, model):
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": self.properties_dict
"$ref": f"#/components/schemas/{SwaggerGenerator.singularise(entity_name)}"
}
}
}
Expand Down Expand Up @@ -119,8 +122,7 @@ def __init__(self, entity_name, model):
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": self.properties_dict
"$ref": f"#/components/schemas/{SwaggerGenerator.singularise(entity_name)}"
}
}
}
Expand All @@ -142,7 +144,7 @@ def __init__(self, entity_name, model):
"content": {
"application/json": {
"schema": {
"type": "object"
"$ref": f"#/components/schemas/{SwaggerGenerator.singularise(entity_name)}"
}
}
}
Expand All @@ -154,8 +156,7 @@ def __init__(self, entity_name, model):
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": self.properties_dict
"$ref": f"#/components/schemas/{SwaggerGenerator.singularise(entity_name)}"
}
}
}
Expand Down Expand Up @@ -208,8 +209,7 @@ def __init__(self, entity_name, model):
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": self.properties_dict
"$ref": f"#/components/schemas/{SwaggerGenerator.singularise(entity_name)}"
}
}
}
Expand Down Expand Up @@ -251,6 +251,7 @@ def __init__(self, entity_name, model):
class SwaggerSpecification(object):
def __init__(self):
self.paths = []
self.schemas = []
self.top_part = {
'openapi': "3.0.0",
"info": {
Expand All @@ -263,16 +264,24 @@ def __init__(self):
"url": "http://localhost:5000"
}
],
"paths": {}
"paths": {},
"components": {
"schemas":{}
}
}

def add_path(self, path):
self.paths.append(path)

def add_schema(self, schema):
self.schemas.append(schema)

def get_spec_as_dict(self):
spec = {}
for path in self.paths:
self.top_part["paths"].update(path)
for schema in self.schemas:
self.top_part["components"]["schemas"].update(schema)
spec.update(self.top_part)

return spec
Expand All @@ -292,6 +301,16 @@ def pascal_to_normal(input):
words = re.findall(r"[A-Z]?[a-z]+|[A-Z]{2,}(?=[A-Z][a-z]|\d|\W|$)|\d+", input)
return " ".join(map(str.lower, words))

@staticmethod
def singularise(plural_word:str):
if plural_word.lower().endswith('ies'):
singular_word = plural_word[:-3] + ('Y' if plural_word.isupper() else 'y')
elif plural_word.lower().endswith('s'):
singular_word = plural_word[:-1]
else:
raise ValueError(f"Don't know how to singularise {plural_word}")
return singular_word

def write_swagger_spec(self):
"""
Writes the openapi.yaml file
Expand All @@ -304,6 +323,7 @@ def write_swagger_spec(self):
swagger_spec.add_path(entity.entity_count_endpoint)
swagger_spec.add_path(entity.entity_id_endpoint)
swagger_spec.add_path(entity.entity_no_id_endpoint)
swagger_spec.add_schema(entity.entity_schema)
swagger_dict = swagger_spec.get_spec_as_dict()
yaml.Dumper.ignore_aliases = lambda *args: True
with open(SwaggerGenerator.FILE_PATH, "w+") as target:
Expand Down

0 comments on commit 762f490

Please sign in to comment.