Skip to content

Enum fields generate default value using string type instead of enum causing pydantic serializer warning #2352

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

Open
devmonkey22 opened this issue Mar 20, 2025 · 0 comments

Comments

@devmonkey22
Copy link

Describe the bug
A generated model with an enum field generates its default value using string type instead of enum value causes pydantic serializer warnings:

.../env/lib/python3.10/site-packages/pydantic/main.py:477: UserWarning: Pydantic serializer warnings:
    Expected `enum` but got `str` with value `'synced'` - serialized value may not be as expected
    return self.__pydantic_serializer__.to_json(

The model output is:

status: Optional[Status] = Field("synced", title="Status")

Manually updating the model output to the following fixes the warning:

status: Optional[Status] = Field(Status.synced, title="Status")

To Reproduce

Example schema:

"components": {
	"schemas": {
		"settings.Setting": {
			"type": "object",
			"properties": {
				"status": {
					"title": "Status",
					"type": "string",
					"enum": [
						"uninitialized",
						"synced",
						"pending",
						"dispatched"
					],
					"default": "synced"
				},
			}
		}
	}
}

Used commandline alternative:

codegen.generate(
	SCHEMA_FILE,
	output=BASE_MODEL_OUTPUT_DIR,

	input_file_type=codegen.InputFileType.OpenAPI,
	output_model_type=codegen.DataModelType.PydanticV2BaseModel,
	target_python_version=codegen.PythonVersion.PY_310,
	base_class="sdk.ProjectBaseModel",
	custom_file_header=file_header,

	use_schema_description=True,
	use_field_description=True,
	use_subclass_enum=True,
	use_double_quotes=True,
	openapi_scopes=[codegen.OpenAPIScope.Schemas]
)

Expected behavior

Output the model with field as:

status: Optional[Status] = Field(Status.synced, title="Status")

Version:

  • OS: Ubuntu 22.04
  • Python version: 3.10
  • datamodel-code-generator version: 0.28.4
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