Skip to content

Issue#26 : New Feature - Option to add custom tag to group Endpoints #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions flask_swagger_generator/generators/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ def wrapper(*args, **kwargs):
return wrapper
return swagger_security

def path_tag(self, tag):
def swagger_path_tag(func):

if not self.generated:
self.specifier.add_path_tag(
func.__name__, tag
)

@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)

return wrapper
return swagger_path_tag

def create_schema(self, reference_name, properties):
return self.specifier.create_schema(reference_name, properties)

Expand All @@ -118,17 +133,16 @@ def index_endpoints(self, app):

for rule in app.url_map.iter_rules():

group = None
function_name = rule.endpoint
if len(rule.endpoint.split(".")) > 1:
group, function_name = rule.endpoint.split('.')
self.specifier.add_endpoint(
for path_tag in self.specifier.path_tags:
if path_tag.get("function_name") == function_name:
group = path_tag.get("tag")
self.specifier.add_endpoint(
function_name=function_name,
path=str(rule),
request_types=rule.methods,
group=group
)
else:
self.specifier.add_endpoint(
function_name=rule.endpoint,
path=str(rule),
request_types=rule.methods,
)
4 changes: 4 additions & 0 deletions flask_swagger_generator/specifiers/swagger_specifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def add_request_body(self, function_name, schema):
def add_security(self, function_name, security_type: SecurityType):
raise NotImplementedError()

@abstractmethod
def add_path_tag(self, function_name, tag):
raise NotImplementedError()

def set_application_name(self, application_name):
self.application_name = application_name

Expand Down
5 changes: 5 additions & 0 deletions flask_swagger_generator/specifiers/swagger_three_specifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ def __init__(self):
self.schemas = []
self.responses = []
self.securities = []
self.path_tags = []

def perform_write(self, file):
# Add all request bodies to request_types with same function name
Expand Down Expand Up @@ -678,6 +679,10 @@ def add_security(self, function_name, security_type: SecurityType):
security_model = SwaggerSecurity([function_name], security_type)
self.securities.append(security_model)

def add_path_tag(self, function_name: str, tag):
path_tag = {"function_name": function_name, "tag": tag}
self.path_tags.append(path_tag)

def create_schema(self, reference_name, properties):
schema = SwaggerSchema(reference_name, properties)
self.schemas.append(schema)
Expand Down
53 changes: 51 additions & 2 deletions tests/resources/reference_version_three.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
openapi: 3.0.1
info:
title: Application
description: Generated at 03/01/2021 20:29:35. This is the swagger
ui based on the open api 3.0 specification of the Application
description: Generated by Flask-Swagger-Generator
version: 1.0.0
externalDocs:
description: Find out more about Swagger
Expand Down Expand Up @@ -75,6 +74,37 @@ paths:
type: integer
description: None
required: True
'/objects02/{object_id}':
get:
tags:
- Object02 Endpoints
operationId: 'retrieve_object02'
responses:
'200':
$ref: '#/components/responses/retrieve_object02_response'
parameters:
- in: path
name: object_id
schema:
type: integer
description: None
required: True
post:
tags:
- Object02 Endpoints
operationId: 'create_object02'
requestBody:
$ref: '#/components/requestBodies/create_object02_request_body'
responses:
'201':
$ref: '#/components/responses/create_object02_response'
parameters:
- in: path
name: object_id
schema:
type: integer
description: None
required: True
components:
securitySchemes:
bearerAuth:
Expand All @@ -96,6 +126,13 @@ components:
application/json:
schema:
$ref: '#/components/schemas/create_object_request_body_schema'
create_object02_request_body:
description: None
required: True
content:
application/json:
schema:
$ref: '#/components/schemas/schema_two'
responses:
retrieve_object_response:
description: retrieve_object response
Expand All @@ -121,6 +158,18 @@ components:
application/json:
schema:
$ref: '#/components/schemas/create_object_response_schema'
retrieve_object02_response:
description: retrieve_object02 response
content:
application/json:
schema:
$ref: '#/components/schemas/schema_two'
create_object02_response:
description: create_object02 response
content:
application/json:
schema:
$ref: '#/components/schemas/schema_two'
schemas:
schema_two:
type: object
Expand Down
23 changes: 20 additions & 3 deletions tests/resources/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ class ObjectDeserializer(Schema):
attribute_six = fields.Nested(ObjectChildDeserializer(many=False))


class TestVersionThreeAPI():
class APITestBase():

def __init__(self):
self.app = create_app()
self.generator = Generator.of(SwaggerVersion.VERSION_THREE)
self.blueprint = Blueprint('objects', __name__)
self.create_test_api()
self.app.register_blueprint(self.blueprint)


class TestVersionThreeAPI(APITestBase):

def create_test_api(self):

generator = self.generator
Expand All @@ -44,7 +47,6 @@ def create_test_api(self):
)
schema_three = generator.create_schema('schema_three', ObjectDeserializer())


@generator.response(200, schema_two)
@blueprint.route('/objects/<int:object_id>', methods=['GET'])
def retrieve_object(object_id, child_id):
Expand All @@ -70,4 +72,19 @@ def update_object(object_id):
@generator.request_body({'id': 10, 'name': 'test_object'})
@blueprint.route('/objects/<int:object_id>', methods=['POST'])
def create_object(object_id):
return jsonify({'objects': []}), 200


@generator.response(200, schema_two)
@generator.path_tag('Object02 Endpoints')
@blueprint.route('/objects02/<int:object_id>', methods=['GET'])
def retrieve_object02(object_id, child_id):
return jsonify({'objects': []}), 200


@generator.response(201, schema_two)
@generator.request_body(schema_two)
@generator.path_tag('Object02 Endpoints')
@blueprint.route('/objects02/<int:object_id>', methods=['POST'])
def create_object02(object_id):
return jsonify({'objects': []}), 200