SolarEdgeTech / pyctuator

Monitor Python applications using Spring Boot Admin
Apache License 2.0
173 stars 17 forks source link

FastAPI 0.89.1 shows warning #87

Closed NGEfreak closed 1 year ago

NGEfreak commented 1 year ago

The following warning is shown using Pyctuator 1.0.1 and FastAPI 0.89.1:

fastapi\utils.py:88: RuntimeWarning: fields may not start with an underscore, ignoring "_links"
    return response_field(field_info=field_info)

FastAPI 0.89 has a new feature which uses the return annotation of a route to create the OpenAPI specification.

The warning is coming from this Pyctuator route:

https://github.com/SolarEdgeTech/pyctuator/blob/2b039ec2095df6f867cd0f6432bbdd0e4976222e/pyctuator/impl/fastapi_pyctuator.py#L45-L47

FastAPI converts the dataclass EndpointsData to a Pydantic model to create the OpenAPI specification. However, Pydantic doesn't allow fields starting with an underscore.

The simplest solution would be to set response_model=None in the route decorator:

@router.get("/", include_in_schema=include_in_openapi_schema, tags=["pyctuator"], response_model=None)
def get_endpoints() -> EndpointsData:
    return self.get_endpoints_data()

This setting has precedence over the return annotation. In FastAPI 0.88 and earlier this was implicitly set to None and the return annotation was not used anyway.

Another solution would be to define EndpointsData as a Pydantic model with an alias definition:

from pydantic import BaseModel, Field

class EndpointsData(BaseModel):
    links: EndpointsLinks = Field(..., alias="_links")
michaelyaakoby commented 1 year ago

Fixed in https://github.com/SolarEdgeTech/pyctuator/pull/91

michaelyaakoby commented 1 year ago

Version 1.0.2