Skip to content

The requestBody declared with $ref is not generating json_body #595

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

Closed
RockyMM opened this issue Mar 31, 2022 · 7 comments · Fixed by #633 or #1056
Closed

The requestBody declared with $ref is not generating json_body #595

RockyMM opened this issue Mar 31, 2022 · 7 comments · Fixed by #633 or #1056
Labels
🐞bug Something isn't working

Comments

@RockyMM
Copy link

RockyMM commented Mar 31, 2022

Describe the bug
If a requestBody is declared with a $ref, then no json_body property is generated. If the reference is inlined, then json_body is generated.

To Reproduce
Steps to reproduce the behaviour:

  1. Example schema:
paths:
  "/objects":
    post:
      operationId: object_create
      requestBody:
        $ref: "#/components/requestBodies/Object"

  1. generated API is:
def sync_detailed(
    *,
    client: Client,
) -> Response[Object]:
    """
    Returns:
        Response[Object]
    """

    kwargs = _get_kwargs(
        client=client,
    )

    response = httpx.request(
        verify=client.verify_ssl,
        **kwargs,
    )

    return _build_response(response=response)

Expected behavior
The expected API should be:

def sync_detailed(
    *,
    client: Client,
    json_body: Object,
) -> Response[Object]:
    """
    Args:
        json_body (Object):

    Returns:
        Response[Object]
    """

    kwargs = _get_kwargs(
        client=client,
        json_body=json_body,
    )

    response = httpx.request(
        verify=client.verify_ssl,
        **kwargs,
    )

    return _build_response(response=response)

OpenAPI Spec File
test.yaml

@RockyMM RockyMM added the 🐞bug Something isn't working label Mar 31, 2022
@kigawas
Copy link
Contributor

kigawas commented Jun 13, 2022

Hi I just submitted a PR above which works with your yaml. It'll be helpful if you have some time to review

Output

def sync_detailed(
    *,
    client: Client,
    json_body: Object,
) -> Response[Object]:
    """
    Args:
        json_body (Object):

    Returns:
        Response[Object]
    """

    kwargs = _get_kwargs(
        client=client,
        json_body=json_body,
    )

    response = httpx.request(
        verify=client.verify_ssl,
        **kwargs,
    )

    return _build_response(response=response)

async def asyncio_detailed(
    *,
    client: Client,
    json_body: Object,
) -> Response[Object]:
    """
    Args:
        json_body (Object):

    Returns:
        Response[Object]
    """

    kwargs = _get_kwargs(
        client=client,
        json_body=json_body,
    )

    async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
        response = await _client.request(**kwargs)

    return _build_response(response=response)

@Leemur89
Copy link

hello, do you have an update on the ongoing PR? I'm facing the exact same issue :/

@kigawas
Copy link
Contributor

kigawas commented Dec 13, 2023

@Leemur89 This library seems not actively maintained. I recommend just using the official openapi generator

@Leemur89
Copy link

@kigawas are you sure? It just got a new release last week
Otherwise do you hav the link to the official openapi generator please? I've been using this one to generate custom python code and could not find a better one
(however I found a workaround to dereference my swagger file)

@kigawas
Copy link
Contributor

kigawas commented Dec 15, 2023

@Leemur89 The biggest problem is that it only supports Open API v3.0 partially and has no plan to upgrade, while fastapi has already shifted to 3.1. If some day I may have to change to use another library, I'd rather change it now.

#585

@dbanty
Copy link
Collaborator

dbanty commented Dec 15, 2023

@Leemur89 The biggest problem is that it only supports Open API v3.0 partially and has no plan to upgrade, while fastapi has already swifted to 3.1. If some day I may have to change to use another library, I'd rather change it now.

3.1 support is in progress in #856 . I would categorize this project as "lightly maintained", I am still working on it but it's (mostly) only me and it's not my focus.

@kigawas
Copy link
Contributor

kigawas commented Dec 15, 2023

@dbanty Glad to hear you are working on this!

github-merge-queue bot pushed a commit that referenced this issue Jun 15, 2024
Fixes #595 

Perhaps similar solution can be applied to #605 

## Description
The requestBody with $ref was not handled. `requestBodies` in components
should be injected into `_add_body` function.

---------

Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>
dbanty added a commit that referenced this issue Jun 15, 2024
This PR was created by Knope. Merging it will create a new release

### Features

#### 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.

### Fixes

- Indent of generated code for non-required lists. Thanks @sfowl!
(#1050)
- Parsing requestBody with $ref (#633)

Co-authored-by: GitHub <github-actions@github.com>
micha91 pushed a commit to micha91/openapi-python-client that referenced this issue May 13, 2025
Fixes openapi-generators#595

Perhaps similar solution can be applied to openapi-generators#605

## Description
The requestBody with $ref was not handled. `requestBodies` in components
should be injected into `_add_body` function.

---------

Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>
micha91 pushed a commit to micha91/openapi-python-client that referenced this issue May 13, 2025
This PR was created by Knope. Merging it will create a new release

### Features

#### 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 openapi-generators#633, closes openapi-generators#664, resolves openapi-generators#595.

### Fixes

- Indent of generated code for non-required lists. Thanks @sfowl!
(openapi-generators#1050)
- Parsing requestBody with $ref (openapi-generators#633)

Co-authored-by: GitHub <github-actions@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞bug Something isn't working
Projects
None yet
4 participants