RWTH-EBC/FiLiP

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

  1. Prohibit overwriting customized ContextAttributes by checking the defined FieldTypes
  2. 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())

@djs0109 This will also require to use the former fiware regex patter. Within in the pattern field of pydantic.field. Meaning that we will set the regex_engine to python for ContextEntity. The current implementation prohibits the documentation of the regex via json_schema export

@djs0109 Hej, if everything is fine now, we will be able to merge, right? I would need this one and the #258 quite urgently.