Skip to content

Commit

Permalink
Merge pull request #308 from WinterFramework/add-url-segment-as-tag
Browse files Browse the repository at this point in the history
Add add_url_segment_as_tag boolean parameter to generate_openapi function to disable adding url segment as tag in OpenAPI
  • Loading branch information
pristupa authored Jul 6, 2024
2 parents 7ac4ecc + acc0619 commit 578fab7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [22.2.0] - 2024-07-06
- Add `add_url_segment_as_tag` boolean parameter to generate_openapi function to disable adding url segment as tag in OpenAPI

## [22.1.1] - 2024-06-17
### Bugfixes
- Add OpenAPI inspector for `@winter.web.query_parameters` case
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![PyPI version](https://badge.fury.io/py/winter.svg)](https://badge.fury.io/py/winter)
[![Gitter](https://badges.gitter.im/winter-python/community.svg)](https://gitter.im/winter-python/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

Web Framework for Python inspired by Spring Framework
Web Framework with focus on python typing, dataclasses and modular design

# Main features
* Declarative API
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "winter"
version = "22.1.1"
version = "22.2.0"
homepage = "https://github.com/WinterFramework/winter"
description = "Web Framework with focus on python typing, dataclasses and modular design"
authors = ["Alexander Egorov <[email protected]>"]
Expand Down
39 changes: 39 additions & 0 deletions tests/winter_openapi/test_add_url_segment_as_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import winter
from winter.web.routing import get_route
from winter_openapi import generate_openapi


def test_add_url_segment_as_tag_false():
class _TestAPI:
@winter.route_get('resource')
def get_resource(self): # pragma: no cover
pass

route = get_route(_TestAPI.get_resource)
# Act
result = generate_openapi(
title='title',
version='1.0.0',
description='description',
routes=[route],
add_url_segment_as_tag=False,
)
# Assert
assert result == {
'components': {'parameters': {}, 'responses': {}, 'schemas': {}},
'info': {'description': 'description', 'title': 'title', 'version': '1.0.0'},
'openapi': '3.0.3',
'paths': {
'/resource': {
'get': {
'deprecated': False,
'operationId': '_TestAPI.get_resource',
'parameters': [],
'responses': {'200': {'description': ''}},
'tags': [],
},
},
},
'servers': [{'url': '/'}],
'tags': []
}
9 changes: 5 additions & 4 deletions winter_openapi/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def generate_openapi(
description: Optional[str] = None,
tags: Optional[List[Dict[str, Any]]] = None,
validate: bool = True,
add_url_segment_as_tag: bool = True,
) -> Dict[str, Any]:
routes = list(routes)
routes.sort(key=lambda r: r.url_path)
Expand All @@ -62,11 +63,11 @@ def generate_openapi(
if not url_path_without_prefix.startswith('/'):
url_path_without_prefix = '/' + url_path_without_prefix

url_path_tag = get_url_path_tag(url_path, path_prefix)
path_tag_names = list(tag_names)

if url_path_tag:
path_tag_names.append(url_path_tag)
if add_url_segment_as_tag:
url_path_tag = get_url_path_tag(url_path, path_prefix)
if url_path_tag:
path_tag_names.append(url_path_tag)

path_item = _get_openapi_path(routes=group_routes, operation_ids=operation_ids, tag_names=path_tag_names)
paths[url_path_without_prefix] = path_item
Expand Down

0 comments on commit 578fab7

Please sign in to comment.