Skip to content

Generated code lacks import of typing.cast for required const properties #1150

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
benweint opened this issue Oct 27, 2024 · 0 comments · Fixed by #1153
Closed

Generated code lacks import of typing.cast for required const properties #1150

benweint opened this issue Oct 27, 2024 · 0 comments · Fixed by #1153

Comments

@benweint
Copy link
Contributor

Describe the bug
When an input schema has a required const property, the generated code does makes calls to typing.cast, but does not import it, leading to failures when trying to run ruff over the generated code.

OpenAPI Spec File

openapi: 3.1.0
info:
  title: Pets Example
  version: 1.0.0
paths:
  /pets:
    post:
      requestBody:
        description: Create a new pet in the system
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Pet"
      responses:
        "200":
          description: Return a 200 status to indicate that the pet was created successfully

components:
  schemas:
    Pet:
      required:
        - type
      properties:
        type:
          const: cat

Desktop (please complete the following information):

  • OS: macOS 14.6.1
  • Python Version: 3.12.6
  • openapi-python-client version: latest main (also present in latest release, 0.21.6)

Additional context

Removing type from the required field list in the example above makes the problem disappear - in this case, cast is still used, but is properly imported.

benweint added a commit to benweint/openapi-python-client that referenced this issue Oct 28, 2024
benweint added a commit to benweint/openapi-python-client that referenced this issue Oct 28, 2024
github-merge-queue bot pushed a commit that referenced this issue Oct 28, 2024
…e template (#1153)

The
[template](https://github.com./openapi-generators/openapi-python-client/blob/main/openapi_python_client/templates/property_templates/const_property.py.jinja#L2)
for `const` properties uses `cast` unconditionally, but the import for
`cast` in `ConstProperty.get_imports` was conditioned on whether the
property was required or not, leading to broken generated code for
required const properties.

I'd be happy to add a test for this if desired! A cursory glance
suggests that maybe the end_to_end tests would be the right place?

Resolves #1150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant