From cb1d50255a4d7277bc9575145fa4d28662ce56e9 Mon Sep 17 00:00:00 2001 From: Thor Whalen Date: Fri, 10 Nov 2023 15:52:31 +0100 Subject: [PATCH] feat: normalized Route.params to json schema --- ju/oas.py | 36 ++++++++++++++++++++++++------------ ju/tests/oas_test.py | 38 +++++++++++++++----------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/ju/oas.py b/ju/oas.py index adf8330..f63397b 100644 --- a/ju/oas.py +++ b/ju/oas.py @@ -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 @@ -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 \ No newline at end of file diff --git a/ju/tests/oas_test.py b/ju/tests/oas_test.py index 10b0db0..f87db07 100644 --- a/ju/tests/oas_test.py +++ b/ju/tests/oas_test.py @@ -1,4 +1,4 @@ -"""Test the util module.""" +"""Test the oas module.""" import yaml import pytest @@ -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'] @@ -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': [] + }