openapi-generators / openapi-generators/openapi-python-client

Missing import when there is a const in the object

Open
#1,074 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2k
Forks
293
Avg merge
34m
Merged PRs (30d)
1

Description

Describe the bug
Version: 0.21.1
When I try to generate a client it fails with the following error:
Error(s) encountered while generating, client was not created

ruff failed

rivery_scim_client/models/book.py:40:21: F821 Undefined name cast
Found 209 errors (208 fixed, 1 remaining).

As you can see there is an import missing. I publish a PR that should solve it.
https://github.com/openapi-generators/openapi-python-client/pull/1072

OpenAPI Spec File
I created a dummy openapi specification.

{
    "openapi": "3.1.0",
    "info": {
        "title": "FastAPI",
        "version": "0.1.0"
    },
    "paths": {
        "/books": {
            "post": {
                "summary": "Create User",
                "operationId": "create_user_books_post",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/Book"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {}
                            }
                        }
                    },
                    "422": {
                        "description": "Validation Error",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/HTTPValidationError"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Book": {
                "properties": {
                    "item_type": {
                        "const": "book",
                        "title": "Item Type",
                        "default": "book"
                    }
                },
                "type": "object",
                "title": "Book"
            },
            "ErrorResponse": {
                "properties": {
                    "schemas": {
                        "items": {
                            "type": "string"
                        },
                        "type": "array",
                        "title": "Schemas",
                        "default": [
                            "urn:ietf:params:scim:api:messages:2.0:Error"
                        ]
                    },
                    "status": {
                        "type": "integer",
                        "title": "Status"
                    },
                    "details": {
                        "type": "string",
                        "title": "Details"
                    }
                },
                "type": "object",
                "required": [
                    "status",
                    "details"
                ],
                "title": "ErrorResponse"
            },
            "HTTPValidationError": {
                "properties": {
                    "detail": {
                        "items": {
                            "$ref": "#/components/schemas/ValidationError"
                        },
                        "type": "array",
                        "title": "Detail"
                    }
                },
                "type": "object",
                "title": "HTTPValidationError"
            },
            "ValidationError": {
                "properties": {
                    "loc": {
                        "items": {
                            "anyOf": [
                                {
                                    "type": "string"
                                },
                                {
                                    "type": "integer"
                                }
                            ]
                        },
                        "type": "array",
                        "title": "Location"
                    },
                    "msg": {
                        "type": "string",
                        "title": "Message"
                    },
                    "type": {
                        "type": "string",
                        "title": "Error Type"
                    }
                },
                "type": "object",
                "required": [
                    "loc",
                    "msg",
                    "type"
                ],
                "title": "ValidationError"
            }
        },
        "securitySchemes": {
            "HTTPBearer": {
                "type": "http",
                "scheme": "bearer"
            }
        }
    }
}

Additional context
Add any other context about the problem here.
N/A

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided OpenAPI specification and inspect the generated rivery_scim_client/models/book.py, especially the undefined cast reference reported at line 40. Compare the behavior with pull request 1072 and verify generation succeeds without the F821 error and that the generated client passes Ruff.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.