Skip to content
Snippets Groups Projects
Commit 95f9c436 authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'auto-x-name' into 'master'

Add x-name on request bodies in manage.py generateschema instead of patch.py

See merge request !603
parents 6df7aad8 be03d894
No related branches found
No related tags found
1 merge request!603Add x-name on request bodies in manage.py generateschema instead of patch.py
......@@ -68,6 +68,17 @@ class AutoSchema(BaseAutoSchema):
}
return schema
def _get_request_body(self, path, method):
"""
Add x-name to requestBody objects to allow APIStar to use `body={}` arguments on requests
"""
request_body = super()._get_request_body(path, method)
if not request_body:
return request_body
assert 'x-name' not in request_body
request_body['x-name'] = 'body'
return request_body
def get_operation(self, path, method):
operation = super().get_operation(path, method)
......
......@@ -282,3 +282,62 @@ class TestOpenAPI(TestCase):
},
'required': ['my_field'],
})
def test_request_body_apistar_compat(self):
"""
Test x-name is added to requestBody to allow APIStar to use the request body as `body={}`
"""
class ThingSerializer(serializers.Serializer):
my_field = serializers.CharField()
class ThingView(APIView):
serializer_class = ThingSerializer
def get_serializer(self):
return self.serializer_class()
def post(self, *args, **kwargs):
pass
inspector = AutoSchema()
inspector.view = create_view(ThingView, 'POST', create_request('/test/'))
self.assertDictEqual(
inspector.get_operation('/test/', 'POST'),
{
'operationId': 'CreateThing',
'parameters': [],
'requestBody': {
'content': {
'application/json': {
'schema': {
'properties': {
'my_field': {
'type': 'string',
},
},
'required': ['my_field']
},
},
},
'x-name': 'body',
},
'responses': {
'200': {
'content': {
'application/json': {
'schema': {
'properties': {
'my_field': {
'type': 'string',
},
},
'required': ['my_field']
},
},
},
'description': '',
}
},
}
)
......@@ -86,22 +86,6 @@ def update_schema(schema, patches):
schema.setdefault('info', {})
schema['info']['version'] = ark_version
# Add x-name property on request body objects to let apistar register the parameters
for path, methods in schema['paths'].items():
for method, operation in methods.items():
if 'requestBody' not in operation:
continue
if not operation['requestBody']['content'].get('application/json', {}).get('schema'):
# Ignore empty schemas
continue
# Make sure we do not duplicate an existing parameter name
assert not any(param['name'] == 'body' for param in operation['parameters']), \
'Operation already has a body parameter'
print('Adding x-name to {} on {}'.format(method, path), file=sys.stderr)
operation['requestBody']['x-name'] = 'body'
return schema
......
......@@ -251,7 +251,6 @@ paths:
type: array
required:
- ids
x-name: body
tags:
- elements
/api/v1/element/{child}/parent/{parent}/:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment