Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add a postprocessing hook for the openAPI spec
Roland Geiderfdf9d8b9b5f0
diff --git a/settings/settings_global.py b/settings/settings_global.pyindex 9ca95d60b..ed1d87067 100644--- a/settings/settings_global.py+++ b/settings/settings_global.py@@ -576,6 +576,13 @@ SPECTACULAR_SETTINGS = {'SWAGGER_UI_FAVICON_HREF': 'SIDECAR','REDOC_DIST': 'SIDECAR','COMPONENT_SPLIT_REQUEST': True,+ # postprocess_schema_enums is spectacular's own default and has to be+ # repeated: assigning the list replaces it, and without it no enum becomes a+ # named component, which renames a good part of every generated client.+ 'POSTPROCESSING_HOOKS': [+ 'drf_spectacular.hooks.postprocess_schema_enums',+ 'wger.utils.api_schema.strip_patch_defaults',+ ],# Both are exposed as a plain "type" field on more than one component, which# spectacular would otherwise name with a hash suffix (e.g. Type947Enum).# The names end up as class names in generated clients, so pin them.diff --git a/wger/utils/api_schema.py b/wger/utils/api_schema.pyindex 417df39de..1d4e97b31 100644--- a/wger/utils/api_schema.py+++ b/wger/utils/api_schema.py@@ -46,3 +46,27 @@ class ImageThumbnailsSerializer(serializers.Serializer):small = ThumbnailAliasSerializer()medium = ThumbnailAliasSerializer()original = serializers.URLField()+++def strip_patch_defaults(result, generator, **kwargs):+ """+ Drops ``default`` from the PATCH request bodies.++ An omitted field in a PATCH means "leave this alone", never "set it to+ this". DRF adds the default so the tuple of a UniqueConstraint can be built+ from a payload that omits one of its fields+ (``ModelSerializer.get_uniqueness_extra_kwargs``), which only applies on+ create: on update the validator reads the missing values off the instance.++ It matters because generated clients send a schema default with every+ request. For ``Measurement.source`` that turns an edit to the note of a+ synced entry into a claim that it was hand-entered.+ """+ for name, component in result.get('components', {}).get('schemas', {}).items():+ if not name.startswith('Patched'):+ continue+ for field in component.get('properties', {}).values():+ if isinstance(field, dict):+ field.pop('default', None)++ return result