Automatically add title and description to generated schema #212
-
Is there a way to add "title" and "description" attributes to the generated schema based on e.g. field name? GUI toolkits like https://github.com/rjsf-team/react-jsonschema-form and https://jsonforms.io/ require "title" attribute to render the form. Pydantic even generates "title" based on the field name if title is not specified explicitly. I wonder if it is possible to automatically add "title" to every property in the generated schema? Maybe using default conversions that would act on every class or add extra_schema? Even better would be if I could add "description" to properties based on comment strings extracted by Sphinx, using the following @functools.lru_cache()
def get_all_field_comments_sphinx(cls: Type) -> Dict[Tuple[str, str], str]:
source = inspect.getsource(cls)
parser = sphinx.pycode.parser.Parser(source)
parser.parse()
return parser.comments
def get_field_comment_sphinx(cls: Type, field_name: str) -> str:
comments = get_all_field_comments_sphinx(cls)
key = (cls.__name__, field_name)
comment = comments.get(key, "")
return comment |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
There are multiple ways, but they are not easy and documented.
That's indeed an issue.
I did want to be as opinionated as Pydantic, and because I had no answer to the question "Should properties' title be capitalized or not?", I've preferred to not add title at all. But I should have given a way to do it easily.
The immediate solution for this is 8 lines of code: def add_titles(schema):
if isinstance(schema, dict):
for name, prop in schema.get("properties", {}).items():
prop["title"] = name.capitalize() # or whatever
for value in schema.values():
add_titles(value)
elif isinstance(schema, list):
for elt in schema:
add_titles(elt) This function can be used to modify in place the schema generated by apischema. But I admit that it's kind of dirty.
from typing import Any, Optional
from apischema import settings, schema
from apischema.objects import object_fields
from apischema.schemas import Schema
prev_default_schema = settings.default_schema
def default_schema(tp: Any) -> Optional[Schema]:
try:
fields = object_fields(tp)
def add_titles(schema: dict[str, Any]):
for field in fields.values():
schema["properties"][field.alias]["title"] = field.alias.capitalize()
# field description could be retrieved and inserted here
return schema(extra=add_titles)
except Exception:
return prev_default_schema(tp)
settings.default_schema = default_schema But this solution is not very convenient too, and That's the two options I've for now. I will think more about it tonight, but I think I have to add something to make object schema customization easier. I should have time to implement something to include it in v0.16 release this weekend. |
Beta Was this translation helpful? Give feedback.
There are multiple ways, but they are not easy and documented.
That's indeed an issue.
I did want to be as opinionated as Pydantic, and because I had no answer to the question "Should properties' title be capitalized or not?", I've preferred to not add title at all. But I should have given a way to do it easily.