Skip to content

Fix naming for nested classes#68

Closed
evroon wants to merge 1 commit intomike-oakley:mainfrom
evroon:fix-nested-classes
Closed

Fix naming for nested classes#68
evroon wants to merge 1 commit intomike-oakley:mainfrom
evroon:fix-nested-classes

Conversation

@evroon
Copy link

@evroon evroon commented May 30, 2025

fixes #67

See the docstrings, for nested classes we need to use qualname instead of name. We also need to prepend the module name, because that's what pydantic does as well.

I also updated the sorting to use __qualname__ instead of __name__.

Can be tested with:

from openapi_pydantic import OpenAPI
from openapi_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
from pydantic import BaseModel, Field


def construct_base_open_api() -> OpenAPI:
    return OpenAPI.model_validate(
        {
            "info": {"title": "My own API", "version": "v0.0.1"},
            "paths": {
                "/a": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "application/json": {
                                    "schema": PydanticSchema(schema_class=ResourceA.Body)
                                }
                            }
                        },
                    }
                },
                "/b": {
                    "post": {
                        "requestBody": {
                            "content": {
                                "application/json": {
                                    "schema": PydanticSchema(schema_class=ResourceB.Body)
                                }
                            }
                        },
                    }
                },
            },
        }
    )


class ResourceA:
    class Body(BaseModel):
        a: str


class ResourceB:
    class Body(BaseModel):
        b: str


open_api = construct_base_open_api()
open_api = construct_open_api_with_schema_class(open_api)

print(open_api.model_dump_json(by_alias=True, exclude_none=True, indent=2))

@mike-oakley
Copy link
Owner

Hey @evroon - thanks for opening this, looks great - would you mind adding a test or two to cover this? 🙏🏼

@mike-oakley mike-oakley added fix A fix for an existing bug change A change to existing behaviour labels May 31, 2025
@evroon
Copy link
Author

evroon commented Jun 6, 2025

Sure, I added a test that covers nesting, let me know what you think of it.

@mike-oakley
Copy link
Owner

Thanks @evroon - looks like the resolver logic has been borked a bit which is breaking the tests. If you can fix the failing checks that would be great and we can get this merged! I rebased to main for you as I've refactored some of the runners for this!

@evroon
Copy link
Author

evroon commented Jul 7, 2025

Hmm I looked into it but I am not sure how to fix the remaining tests.

The paths contain references to#/components/schemas/tests__v3_0__test_validated_schema__construct_sample_api___locals___SampleRequest but the definitions are called #/components/schemas/SampleRequest. Not sure where the tests__v3_0__test_validated_schema__construct_sample_api___locals___SampleRequest comes from.

@evroon evroon force-pushed the fix-nested-classes branch from 8534dfa to 1cffc21 Compare July 14, 2025 14:45
@evroon evroon force-pushed the fix-nested-classes branch from 1cffc21 to 2deae74 Compare July 14, 2025 14:46
@evroon
Copy link
Author

evroon commented Sep 3, 2025

I don't think this is actually possible because it depends on Rust code in pydantic-core which is responsible for the naming of these classes. That naming logic can also be changed in newer versions of pydantic-core so even if you get it right in this PR, it might be broken in the future. I think it makes more sense to just consider nested classes a limitation of this project.

@evroon evroon closed this Sep 5, 2025
@evroon evroon deleted the fix-nested-classes branch September 5, 2025 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change A change to existing behaviour fix A fix for an existing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested classes result in invalid openapi.json

2 participants