Skip to content

Commit e4064e7

Browse files
authored
Fix method parameters generate schema. (#23)
* Fix method parameters generate schema.
1 parent 8194799 commit e4064e7

17 files changed

Lines changed: 172 additions & 65 deletions

File tree

.pre-commit-config.yaml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ repos:
1414
- id: check-builtin-literals
1515
- id: check-toml
1616
- id: double-quote-string-fixer
17+
- id: check-merge-conflict
18+
- repo: https://github.com/asottile/pyupgrade
19+
rev: v3.21.2
20+
hooks:
21+
- id: pyupgrade
22+
args: [--py314-plus]
1723
- repo: https://github.com/pre-commit/pygrep-hooks
1824
rev: v1.10.0
1925
hooks:
@@ -24,15 +30,18 @@ repos:
2430
- id: python-no-log-warn
2531
- id: python-use-type-annotations
2632
- id: text-unicode-replacement-char
33+
- repo: https://github.com/dannysepler/rm_unneeded_f_str
34+
rev: v0.2.0
35+
hooks:
36+
- id: rm-unneeded-f-str
37+
- repo: https://github.com/mxr/unkey
38+
rev: v0.0.2
39+
hooks:
40+
- id: unkey
2741
- repo: https://github.com/pycqa/isort
28-
rev: 9.0.0a3
42+
rev: 9.0.0b1
2943
hooks:
3044
- id: isort
31-
- repo: https://github.com/asottile/pyupgrade
32-
rev: v3.21.2
33-
hooks:
34-
- id: pyupgrade
35-
args: [--py314-plus]
3645
- repo: https://github.com/hauntsaninja/no_implicit_optional
3746
rev: '1.4'
3847
hooks:
@@ -59,6 +68,20 @@ repos:
5968
- flake8-pytest-style
6069
# - flake8-absolute-import
6170
args: [--max-line-length=100]
71+
#- repo: local
72+
# hooks:
73+
# - id: pylint
74+
# name: pylint
75+
# entry: pylint
76+
# language: system
77+
# types: [python]
78+
# require_serial: true
79+
- repo: https://github.com/PyCQA/bandit
80+
rev: 1.9.4
81+
hooks:
82+
- id: bandit
83+
args: [-c, pyproject.toml]
84+
additional_dependencies: ['bandit[toml]']
6285
- repo: https://github.com/mrtazz/checkmake.git
6386
rev: v0.3.0
6487
hooks:

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Version 0.14.3
2+
3+
* Fix method parameters generate schema.
4+
15
## Version 0.14.2
26

37
* Fix parameters generate schema.

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ license-files = ["LICENSE.txt"]
2020
name = "Schema-First"
2121
readme = "README.md"
2222
requires-python = ">=3.14"
23-
version = "0.14.2"
23+
version = "0.14.3"
2424

2525
[project.optional-dependencies]
2626
dev = [
@@ -38,6 +38,9 @@ dev = [
3838
changelog = "https://github.com/flask-pro/schema-first/blob/master/CHANGES.md"
3939
repository = "https://github.com/flask-pro/schema-first"
4040

41+
[tool.bandit]
42+
exclude_dirs = ["tests"]
43+
4144
[tool.black]
4245
extend-exclude = '''
4346
# A regex preceded with ^/ will apply only to files and directories

src/schema_first/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .specification import Specification
1+
from schema_first.specification import Specification
22

33
__all__ = ['Specification']
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .yaml_loader import load_from_yaml
1+
from schema_first.loaders.yaml_loader import load_from_yaml
22

33
__all__ = ['load_from_yaml']

src/schema_first/loaders/exc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ..exceptions import SchemaFirstException
1+
from schema_first.exceptions import SchemaFirstException
22

33

44
class YAMLReaderError(SchemaFirstException):

src/schema_first/loaders/yaml_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import yaml
88

9-
from .exc import ResolverError
10-
from .exc import YAMLReaderError
9+
from schema_first.loaders.exc import ResolverError
10+
from schema_first.loaders.exc import YAMLReaderError
1111

1212

1313
class YAMLReader:

src/schema_first/openapi/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
from marshmallow import ValidationError
55

6-
from ..loaders.yaml_loader import load_from_yaml
7-
from .exc import OpenAPIValidationError
8-
from .schemas.v3_2.openapi_object_schema import OpenAPIObjectSchema
6+
from schema_first.loaders.yaml_loader import load_from_yaml
7+
from schema_first.openapi.exc import OpenAPIValidationError
8+
from schema_first.openapi.schemas.v3_2.openapi_object_schema import OpenAPIObjectSchema
99

1010

1111
class OpenAPI:

src/schema_first/openapi/schemas/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Meta:
1515
def validate_ref(self, data, **kwargs) -> None:
1616
if 'ref' in data:
1717
ALLOWED_FIELDS = {'ref', 'description', 'summary'}
18-
ALL_FIELDS = set(data.keys())
18+
ALL_FIELDS = set(data)
1919
if ALL_FIELDS.difference(ALLOWED_FIELDS):
2020
raise ValidationError(
2121
f"If there is a <'ref'> field, then only <{ALLOWED_FIELDS}>,"

src/schema_first/specification/spilli_api.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ def __init__(self, open_api_spec: dict) -> None:
1111
def _convert_params_to_schema(
1212
self, params: list[dict], place: t.Literal['header', 'cookie', 'path', 'query']
1313
) -> Any | None:
14-
schema = {
15-
'type': 'object',
16-
'additionalProperties': True,
17-
}
14+
schema = {'type': 'object', 'additionalProperties': True}
1815

1916
required = []
2017
properties = {}
@@ -47,18 +44,22 @@ def _params_list_to_dict(self, params: list[dict]) -> dict:
4744

4845
return params_as_dict
4946

50-
def _make_to_spilli_api_format(self) -> None:
51-
self.spilli_api_spec = json.loads(json.dumps(self.open_api_spec))
52-
47+
def _convert_method_params(self) -> None:
5348
for _, methods in self.spilli_api_spec['paths'].items():
54-
common_params = []
55-
if 'parameters' in methods:
56-
common_params = methods.pop('parameters', [])
49+
common_params = methods.pop('parameters', [])
50+
51+
for method, operation_obj in methods.items():
52+
if method in ('summary', 'description', 'servers'):
53+
continue
5754

58-
for _, operation_obj in methods.items():
59-
if 'parameters' in operation_obj:
60-
method_params = common_params + operation_obj.pop('parameters', [])
61-
operation_obj['parameters'] = self._params_list_to_dict(method_params)
55+
method_params = operation_obj.get('parameters', [])
56+
all_method_params = common_params + method_params
57+
if all_method_params:
58+
operation_obj['parameters'] = self._params_list_to_dict(all_method_params)
59+
60+
def _make_to_spilli_api_format(self) -> None:
61+
self.spilli_api_spec = json.loads(json.dumps(self.open_api_spec))
62+
self._convert_method_params()
6263

6364
def convert(self) -> dict:
6465
self._make_to_spilli_api_format()

0 commit comments

Comments
 (0)