Skip to content
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

Incorrect const property validation #1044

Open
dthung1602 opened this issue May 14, 2024 · 1 comment
Open

Incorrect const property validation #1044

dthung1602 opened this issue May 14, 2024 · 1 comment

Comments

@dthung1602
Copy link

Describe the bug

The const property validation introduced in #1024 is not correct when there are >= 2 non-none values.

In particular, the validation accepts only the first value

OpenAPI Spec File

--> Please focus on the Foo schema at the end

{
  "openapi": "3.1.0",
  "info": {
    "title": "Test Service",
    "version": "0.1.0"
  },
  "paths": {
    "/test": {
      "post": {
        "summary": "Test",
        "operationId": "test",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TestModel"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "TestModel": {
        "properties": {
          "foo": {
            "$ref": "#/components/schemas/Foo"
          }
        },
        "type": "object",
        "required": [
          "foo"
        ],
        "title": "TestModel"
      },
      "Foo": {
        "oneOf": [
          {
            "const": 1
          },
          {
            "const": 2
          }
        ],
        "title": "FooType"
      }
    }
  }
}

Desktop (please complete the following information):

Additional context

The above schema generates the file test-service-client/test_service_client/models/test_model.py as follows:

class TestModel:
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()

        def _parse_foo(data: object) -> Union[Literal[1], Literal[2]]:
            componentsschemas_foo_type_0 = cast(Literal[1], data)
            if componentsschemas_foo_type_0 != 1:
                raise ValueError(
                    f"/components/schemas/Foo_type_0 must match const 1, got '{componentsschemas_foo_type_0}'"
                )
            return componentsschemas_foo_type_0
            
            # -----> ERROR HERE: the code below this line is never reached<------
       
            componentsschemas_foo_type_1 = cast(Literal[2], data)
            if componentsschemas_foo_type_1 != 2:
                raise ValueError(
                    f"/components/schemas/Foo_type_1 must match const 2, got '{componentsschemas_foo_type_1}'"
                )
            return componentsschemas_foo_type_1

        foo = _parse_foo(d.pop("foo"))

        test_model = cls(
            foo=foo,
        )

        test_model.additional_properties = d
        return test_model
@dthung1602
Copy link
Author

I can open a PR to fix this bug if you like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant