Skip to content

Commit

Permalink
feat: normalized Route.params to json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Nov 10, 2023
1 parent f82d14a commit cb1d502
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
36 changes: 24 additions & 12 deletions ju/oas.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Route:
>>> route_get.method_data['summary']
'List items'
>>> route_get.params
[{'in': 'query', 'name': 'type', 'schema': {'type': 'string'}, 'required': True, 'description': 'Type of items to list'}]
{'type': 'object', 'properties': {'type': {'type': 'string'}}, 'required': ['type']}
"""

method: str
Expand Down Expand Up @@ -168,19 +168,31 @@ def params(self):
(it should usually just be one or the other, not both).
We're calling this 'params' because that's what FastAPI calls it.
"""
# Start with the parameters defined in the 'parameters' section
p = self.method_data.get('parameters', [])
schema = {
'type': 'object',
'properties': {},
'required': []
}

# Process query and path parameters
for param in self.method_data.get('parameters', []):
# Add each parameter to the properties
schema['properties'][param['name']] = param.get('schema', {})

# Mark as required if specified
if param.get('required', False):
schema['required'].append(param['name'])

# Check if requestBody is defined and has content with a JSON content type
# Process requestBody
request_body = self.method_data.get('requestBody', {})
content = request_body.get('content', {}).get('application/json', {})

# If there's a schema, we extract its properties and merge with the parameters
if 'schema' in content:
schema_props = content['schema'].get('properties', {})
for name, details in schema_props.items():
p.append(
{'in': 'requestBody', 'name': name, 'schema': details,}
)
# Merge the requestBody schema with the existing properties
body_schema = content['schema']
schema['properties'].update(body_schema.get('properties', {}))

# Add required properties from requestBody
if 'required' in body_schema:
schema['required'].extend(body_schema['required'])

return p
return schema
38 changes: 15 additions & 23 deletions ju/tests/oas_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Test the util module."""
"""Test the oas module."""

import yaml
import pytest
Expand Down Expand Up @@ -79,22 +79,14 @@ def test_routes(openapi_spec):
}
}

assert get_route.params == [
{
'in': 'query',
'name': 'type',
'schema': {'type': 'string'},
'required': True,
'description': 'Type of items to list',
},
{
'in': 'query',
'name': 'limit',
'schema': {'type': 'integer', 'default': 10},
'required': False,
'description': 'Maximum number of items to return',
assert get_route.params == {
'type': 'object',
'properties': {
'type': {'type': 'string'},
'limit': {'type': 'integer', 'default': 10},
},
]
'required': ['type']
}

post_route = routes['post', '/items']

Expand Down Expand Up @@ -135,11 +127,11 @@ def test_routes(openapi_spec):
}
}

assert post_route.params == [
{'in': 'requestBody', 'name': 'name', 'schema': {'type': 'string'}},
{
'in': 'requestBody',
'name': 'age',
'schema': {'type': 'integer', 'default': 42},
assert post_route.params == {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'age': {'type': 'integer', 'default': 42},
},
]
'required': []
}

0 comments on commit cb1d502

Please sign in to comment.