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

Default value of enum field isn't an enum member #1929

Closed
Twixes opened this issue Apr 19, 2024 · 2 comments
Closed

Default value of enum field isn't an enum member #1929

Twixes opened this issue Apr 19, 2024 · 2 comments
Labels

Comments

@Twixes
Copy link

Twixes commented Apr 19, 2024

Describe the bug

This is similar to #1797, but specifically about enums. When running datamodel-codegen on the schema below, the result includes

class PropertyOperator(str, Enum):
    exact = "exact"
    is_not = "is_not"
    icontains = "icontains"
    not_icontains = "not_icontains"

class EventPropertyFilter(BaseModel):
    operator: Optional[PropertyOperator] = "exact"

This is invalid, because "exact" is not an instance of PropertyOperator.

To Reproduce

Example schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "EventPropertyFilter": {
            "properties": {
                "operator": {
                    "$ref": "#/definitions/PropertyOperator",
                    "default": "exact"
                }
            },
            "required": ["operator"],
            "type": "object"
        },
        "PropertyOperator": {
            "enum": [
                "exact",
                "is_not",
                "icontains",
                "not_icontains"
            ],
            "type": "string"
        }
    }
}

Used commandline:

$ datamodel-codegen \
    --class-name='SchemaRoot' --collapse-root-models --target-python-version 3.10 --disable-timestamp \
    --use-one-literal-as-default --use-default --use-default-kwarg --use-subclass-enum \
    --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel

Expected behavior

The output should be this:

 class EventPropertyFilter(BaseModel):
-    operator: Optional[PropertyOperator] = "exact"
+    operator: Optional[PropertyOperator] = PropertyOperator("exact")

Version:

  • OS: macOS 14.4.1
  • Python version: 3.10
  • datamodel-code-generator version: 0.25.5
@koxudaxi
Copy link
Owner

@Twixes
We have the --set-default-enum-member to set the enum member as the default value.

 --set-default-enum-member
                        Set enum members as default values for enum field
...

class EventPropertyFilter(BaseModel):
    operator: Optional[PropertyOperator] = PropertyOperator.exact

@Twixes
Copy link
Author

Twixes commented May 3, 2024

Thank you! I missed --set-default-enum-member completely, but it's perfect.

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

No branches or pull requests

2 participants