Make ContextEntity more Customizable
tstorek opened this issue · 3 comments
Is your feature request related to a problem? Please describe.
Currently, subsclassing of ContextEntity is rather difficult because the Model tends to overwrite customized subslasses of ContextAtrributes
with ContextAttribute
-Type. This acutally should only happen if a user does not know the customized subclasses.
However, if one want's to implement a RestAPI with more specific types you usually need to subclass and define stricter models as the generic ones.
Furthermore, schema-export only returns unspecific schemas. That is not what you want when providing a service.
Describe the solution you'd like
- Prohibit overwriting customized ContextAttributes by checking the defined FieldTypes
- Add example of how to properly inherit ContextEntity and specify custom
ContextAttribute
-types for proper schema generation.
Describe alternatives you've considered
Write your own intermediate class that allows for the functionality described above.
Example*
from pprint import pprint
from pydantic.fields import FieldInfo
class WeatherStation(ContextEntity):
"""
A dedicated weather station.
https://brickschema.org/schema/Brick#Weather_Station
"""
id: str = FieldInfo.merge_field_infos(
ContextEntity.model_fields["id"],
title="ID of the Weather Station",
frozen=False,
examples=["ngsi-ld:WeatherStation:001"],
description="ID of the Weather Station. Allowed characters are the "
"ones in the plain ASCII set, except the following ones: "
"control characters, whitespace, &, ?, / and #."
)
type: str = FieldInfo.merge_field_infos(
ContextEntity.model_fields["type"],
title="Type of the Weather Station",
default="brick:Weather_Station",
validate_default=True,
frozen=False,
examples=["brick:Weather_Station"],
description="Type of the Weather Station. You can leave it to the "
"default value if you are using the Brick models. Allowed "
"characters are the ones in the plain ASCII set, except "
"the following ones: control characters, whitespace, &, "
"?, / and #."
)
pprint(WeatherStation.model_schema_json())
Branch 225-Make-ContextEntity-more-Customizable created!