Skip to content

fix: Parsing requestBody with $ref #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .changeset/support_request_body_refs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
default: minor
---

# Support request body refs

You can now define and reuse bodies via refs, with a document like this:

```yaml
paths:
/something:
post:
requestBody:
"$ref": "#/components/requestBodies/SharedBody"
components:
requestBodies:
SharedBody:
content:
application/json:
schema:
type: string
```

Thanks to @kigawas and @supermihi for initial implementations and @RockyMM for the initial request.

Closes #633, closes #664, resolves #595.
44 changes: 44 additions & 0 deletions end_to_end_tests/__snapshots__/test_end_to_end.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# serializer version: 1
# name: test_documents_with_errors[circular-body-ref]
'''
Generating /test-documents-with-errors
Warning(s) encountered while generating. Client was generated, but some pieces may be missing

WARNING parsing POST / within default. Endpoint will not be generated.

Circular $ref in request body


If you believe this was a mistake or this tool is missing a feature you need, please open an issue at https://github.com/openapi-generators/openapi-python-client/issues/new/choose

'''
# ---
# name: test_documents_with_errors[missing-body-ref]
'''
Generating /test-documents-with-errors
Warning(s) encountered while generating. Client was generated, but some pieces may be missing

WARNING parsing POST / within default. Endpoint will not be generated.

Could not resolve $ref #/components/requestBodies/body in request body


If you believe this was a mistake or this tool is missing a feature you need, please open an issue at https://github.com/openapi-generators/openapi-python-client/issues/new/choose

'''
# ---
# name: test_documents_with_errors[optional-path-param]
'''
Generating /test-documents-with-errors
Warning(s) encountered while generating. Client was generated, but some pieces may be missing

WARNING parsing GET /{optional} within default. Endpoint will not be generated.

Path parameter must be required

Parameter(name='optional', param_in=<ParameterLocation.PATH: 'path'>, description=None, required=False, deprecated=False, allowEmptyValue=False, style=None, explode=False, allowReserved=False, param_schema=Schema(title=None, multipleOf=None, maximum=None, exclusiveMaximum=None, minimum=None, exclusiveMinimum=None, maxLength=None, minLength=None, pattern=None, maxItems=None, minItems=None, uniqueItems=None, maxProperties=None, minProperties=None, required=None, enum=None, const=None, type=<DataType.STRING: 'string'>, allOf=[], oneOf=[], anyOf=[], schema_not=None, items=None, properties=None, additionalProperties=None, description=None, schema_format=None, default=None, nullable=False, discriminator=None, readOnly=None, writeOnly=None, xml=None, externalDocs=None, example=None, deprecated=None), example=None, examples=None, content=None)

If you believe this was a mistake or this tool is missing a feature you need, please open an issue at https://github.com/openapi-generators/openapi-python-client/issues/new/choose

'''
# ---
31 changes: 31 additions & 0 deletions end_to_end_tests/baseline_openapi_3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@
}
}
},
"/bodies/refs": {
"post": {
"tags": [
"bodies"
],
"description": "Test request body defined via ref",
"operationId": "refs",
"requestBody": {
"$ref": "#/components/requestBodies/NestedRef"
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/tests/": {
"get": {
"tags": [
Expand Down Expand Up @@ -2761,6 +2778,20 @@
"type": "string"
}
}
},
"requestBodies": {
"NestedRef": {
"$ref": "#/components/requestBodies/ARequestBody"
},
"ARequestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AModel"
}
}
}
}
}
}
}
31 changes: 27 additions & 4 deletions end_to_end_tests/baseline_openapi_3.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ info:
}
}
},
"/bodies/refs": {
"post": {
"tags": [
"bodies"
],
"description": "Test request body defined via ref",
"operationId": "refs",
"requestBody": {
"$ref": "#/components/requestBodies/NestedRef"
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/tests/": {
"get": {
"tags": [
Expand Down Expand Up @@ -1604,7 +1621,7 @@ info:
}
}
}
"components": {
"components":
"schemas": {
"AFormData": {
"type": "object",
Expand Down Expand Up @@ -2704,7 +2721,7 @@ info:
}
}
}
},
}
"parameters": {
"integer-param": {
"name": "integer param",
Expand Down Expand Up @@ -2772,5 +2789,11 @@ info:
}
}
}
}

requestBodies:
NestedRef:
"$ref": "#/components/requestBodies/ARequestBody"
ARequestBody:
content:
"application/json":
"schema":
"$ref": "#/components/schemas/AModel"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import types

from . import json_like, post_bodies_multiple
from . import json_like, post_bodies_multiple, refs


class BodiesEndpoints:
Expand All @@ -19,3 +19,10 @@ def json_like(cls) -> types.ModuleType:
A content type that works like json but isn't application/json
"""
return json_like

@classmethod
def refs(cls) -> types.ModuleType:
"""
Test request body defined via ref
"""
return refs
20 changes: 20 additions & 0 deletions end_to_end_tests/documents_with_errors/circular-body-ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
openapi: "3.1.0"
info:
title: "Circular Body Ref"
version: "0.1.0"
paths:
/:
post:
requestBody:
$ref: "#/components/requestBodies/body"
responses:
"200":
description: "Successful Response"
content:
"application/json":
schema:
const: "Why have a fixed response? I dunno"
components:
requestBodies:
body:
$ref: "#/components/requestBodies/body"
16 changes: 16 additions & 0 deletions end_to_end_tests/documents_with_errors/missing-body-ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: "3.1.0"
info:
title: "Trying to use a request body ref that does not exist"
version: "0.1.0"
paths:
/:
post:
requestBody:
$ref: "#/components/requestBodies/body"
responses:
"200":
description: "Successful Response"
content:
"application/json":
schema:
const: "Why have a fixed response? I dunno"
103 changes: 103 additions & 0 deletions end_to_end_tests/golden-record/my_test_api_client/api/bodies/refs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from http import HTTPStatus
from typing import Any, Dict, Optional, Union

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.a_model import AModel
from ...types import Response


def _get_kwargs(
*,
body: AModel,
) -> Dict[str, Any]:
headers: Dict[str, Any] = {}

_kwargs: Dict[str, Any] = {
"method": "post",
"url": "/bodies/refs",
}

_body = body.to_dict()

_kwargs["json"] = _body
headers["Content-Type"] = "application/json"

_kwargs["headers"] = headers
return _kwargs


def _parse_response(*, client: Union[AuthenticatedClient, Client], response: httpx.Response) -> Optional[Any]:
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(*, client: Union[AuthenticatedClient, Client], response: httpx.Response) -> Response[Any]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: AModel,
) -> Response[Any]:
"""Test request body defined via ref

Args:
body (AModel): A Model for testing all the ways custom objects can be used

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Any]
"""

kwargs = _get_kwargs(
body=body,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


async def asyncio_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: AModel,
) -> Response[Any]:
"""Test request body defined via ref

Args:
body (AModel): A Model for testing all the ways custom objects can be used

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Any]
"""

kwargs = _get_kwargs(
body=body,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)
14 changes: 10 additions & 4 deletions end_to_end_tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,18 @@ def test_bad_url():
assert "Could not get OpenAPI document from provided URL" in result.stdout


def test_invalid_document():
ERROR_DOCUMENTS = [path for path in Path(__file__).parent.joinpath("documents_with_errors").iterdir() if path.is_file()]


@pytest.mark.parametrize("document", ERROR_DOCUMENTS, ids=[path.stem for path in ERROR_DOCUMENTS])
def test_documents_with_errors(snapshot, document):
runner = CliRunner()
path = Path(__file__).parent / "invalid_openapi.yaml"
result = runner.invoke(app, ["generate", f"--path={path}", "--fail-on-warning"])
output_path = Path.cwd() / "test-documents-with-errors"
shutil.rmtree(output_path, ignore_errors=True)
result = runner.invoke(app, ["generate", f"--path={document}", "--fail-on-warning", f"--output-path={output_path}"])
assert result.exit_code == 1
assert "Warning(s) encountered while generating" in result.stdout
assert result.stdout.replace(str(output_path), "/test-documents-with-errors") == snapshot
shutil.rmtree(output_path, ignore_errors=True)


def test_custom_post_hooks():
Expand Down
Loading
Loading