[jsonschemagen] Add lifecycle methods to jsonschemagen #2369
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related to: #2339
Lifecycle methods in generators are a great way to allow people to modify the generation process in ways that might not have a place in the main in-library generators while not having to override methods that would cause subclasses to get out of date with changes in in-library generators.
In this case the request was to add special terms for a json schema dialect for UI elements. Adding lifecycle hooks allows them to experiment with making that work - and if it is something that we might want to integrate upstream then we have a clear, packaged way of doing that that cleanly divides the additional parts into separate methods rather than intercalating them in the other parts of the generator.
As usual, i could only implement some of the lifecycle hooks (eg. the
after
methods for the collectiveclasses
methods can't be done because the results aren't returned, but directly added to a shared dict), but i added which to the docstring.Added
before/after_generate_class_slot
because those seem different enough from generating a slot by itself to be worth a separate method that also passes the class that the slot is being built in the context of. will save swapping pydanticgen over to those for another PR.Build results classes here are trivial since jsonschemagen doesn't use the builder pattern, used
model_construct
since there's no actual validation to be done and i wasn't sure if the pydantic validator would take extra time with a customdict
subclass.