Skip to content

Unable to use field_serializer with fields allowed to be only a Point #156

@void-rooster

Description

@void-rooster

We have to provide coordinates with 6 digits of precision and use a field serializer to do so. Curiously, we can use the serializer on fields allowed to be either a Point or a Polygon, but not with fields allowed to be only a Point.

This produces a SchemaError:

from pydantic import BaseModel, field_serializer
from geojson_pydantic import Point

class Test(BaseModel):
    point: Point
    @field_serializer("point")
    def six_digits_precision(self, pt: Point) -> Point:
        rounded_coords = [round(coord, 6) for coord in pt.coordinates]
        return Point(type="Point", coordinates=rounded_coords)

traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/kpenner/mambaforge/envs/pydantic/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 183, in __new__
    complete_model_class(
  File "/home/kpenner/mambaforge/envs/pydantic/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 535, in complete_model_class
    cls.__pydantic_validator__ = create_schema_validator(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kpenner/mambaforge/envs/pydantic/lib/python3.12/site-packages/pydantic/plugin/_schema_validator.py", line 49, in create_schema_validator
    return SchemaValidator(schema, config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.SchemaError: Definitions error: definition `geojson_pydantic.geometries.Point:94014434282928` was never filled

without the field serializer, or with the field serializer and different treatment for coordinate extraction based on Point vs Polygon, it succeeds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions