From 4d4f3f5035d3d29ba793659662339f2e2e10e7df Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Sun, 15 Aug 2021 14:37:23 +0300 Subject: [PATCH] Fix bug issues with duplicate params and null data (#246) * Fix bug issues with duplicate params and null data * bump version --- sanic_openapi/__init__.py | 2 +- sanic_openapi/openapi3/blueprint.py | 8 ++++++++ sanic_openapi/openapi3/definitions.py | 2 ++ sanic_openapi/openapi3/types.py | 15 +++++++++++++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/sanic_openapi/__init__.py b/sanic_openapi/__init__.py index 2dab60d..23a4639 100644 --- a/sanic_openapi/__init__.py +++ b/sanic_openapi/__init__.py @@ -3,7 +3,7 @@ swagger_blueprint = openapi2_blueprint -__version__ = "21.6.0" +__version__ = "21.6.1" __all__ = [ "openapi2_blueprint", "swagger_blueprint", diff --git a/sanic_openapi/openapi3/blueprint.py b/sanic_openapi/openapi3/blueprint.py index 6757366..343d662 100644 --- a/sanic_openapi/openapi3/blueprint.py +++ b/sanic_openapi/openapi3/blueprint.py @@ -93,6 +93,14 @@ def build_spec(app, loop): # ) for _parameter in route_parameters: + if any( + ( + param.fields["name"] == _parameter.name + for param in operation.parameters + ) + ): + continue + operation.parameter( _parameter.name, _parameter.cast, "path" ) diff --git a/sanic_openapi/openapi3/definitions.py b/sanic_openapi/openapi3/definitions.py index 7311657..156e4ed 100644 --- a/sanic_openapi/openapi3/definitions.py +++ b/sanic_openapi/openapi3/definitions.py @@ -171,6 +171,8 @@ class Parameter(Definition): deprecated: Optional[bool] allowEmptyValue: Optional[bool] + __nullable__ = None + def __init__( self, name: str, diff --git a/sanic_openapi/openapi3/types.py b/sanic_openapi/openapi3/types.py index 171bf7e..f181c1c 100644 --- a/sanic_openapi/openapi3/types.py +++ b/sanic_openapi/openapi3/types.py @@ -3,11 +3,12 @@ from datetime import date, datetime, time from enum import Enum from inspect import isclass -from typing import Any, Dict, List, Union, get_type_hints +from typing import Any, Dict, List, Optional, Union, get_type_hints class Definition: __fields: dict + __nullable__: Optional[List[str]] = [] def __init__(self, **kwargs): self.__fields = self.guard(kwargs) @@ -24,7 +25,17 @@ def guard(self, fields): } def serialize(self): - return _serialize(self.fields) + return { + k: v + for k, v in _serialize(self.fields).items() + if ( + v + or ( + isinstance(self.__nullable__, list) + and (not self.__nullable__ or k in self.__nullable__) + ) + ) + } def __str__(self): return json.dumps(self.serialize())