litestar-org/litestar

SerializationException: Unsupported type: <class 'geoalchemy2.elements.WKBElement'>

Closed this issue · 0 comments

Description

Hi, I am using MySQL as the database. I am representing the geometry values in the model using GeoAlchemy2. When I list the geometry columns in the model, I get the error SerializationException: Unsupported type: <class 'geoalchemy2.elements.WKBElement'>.

URL to code causing the issue

raise SerializationException(str(msgspec_error)) from msgspec_error

MCVE

# Create point on geometry
from geoalchemy2 import Geometry

class Point(Geometry):
    def __init__(self, *args, **kwargs):
        self.geometry_type = "POINT"
        self.cache_ok = True
        super().__init__(*args, **kwargs)

# Create a model
class Model(BigIntAuditBase):
    __tablename__ = "model"

    coordinate: Mapped[Point] = mapped_column(Point(srid=4326), nullable=True)

# Create a openapi schema plugin
config = OpenAPIConfig(
    title=settings.app.NAME,
    version=current_version,
    use_handler_docstrings=True,
    render_plugins=[ScalarRenderPlugin()],
)

coordinate = Schema(
    type=OpenAPIType.ARRAY,
    description="Coordinates.",
    items=Schema(type=OpenAPIType.NUMBER),
    min_length=2,
    max_length=3,
)

class GeoalchemySchemaPlugin(OpenAPISchemaPlugin):
    def is_plugin_supported_field(self, field_definition: FieldDefinition) -> bool:
        return field_definition in (Geometry, Point, LineString, Polygon)

    def to_openapi_schema(
        self, field_definition: FieldDefinition, schema_creator: SchemaCreator
    ) -> Schema:
        return Schema(
            type=OpenAPIType.OBJECT,
            properties={
                "type": Schema(
                    type=OpenAPIType.STRING,
                    default="Point",
                    description="Geometry Type.",
                ),
                "coordinates": Schema(
                    type=OpenAPIType.STRING,
                    default="Point",
                    description="Geometry Type.",
                ),
            },
        )
  

Litestar(
        ...
        plugins=[
            ...,
            GeoalchemySchemaPlugin()
        ],
    )

Steps to reproduce

1. Import the Geometry class from GeoAlchemy2.
2. Create a new Point class that inherits from the Geometry class.
3. Set the column's data type to Point in the relevant model.
4. Create an OpenAPI schema plugin.
5. Send a GET request to the relevant model.

Screenshots

No response

Logs

No response

Litestar Version

2.9.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar