Allow primary key for nested models in OpenAPI request schemas (#18451)

This commit is contained in:
Marcus Weiner 2025-03-05 20:46:12 +01:00 committed by GitHub
parent ed6ccfb723
commit 631ff3e702
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -295,3 +295,23 @@ class FixIntegerRangeSerializerSchema(OpenApiSerializerExtension):
'maxItems': 2, 'maxItems': 2,
}, },
} }
# Nested models can be passed by ID in requests
# The logic for this is handled in `BaseModelSerializer.to_internal_value`
class FixWritableNestedSerializerAllowPK(OpenApiSerializerFieldExtension):
target_class = 'netbox.api.serializers.BaseModelSerializer'
match_subclasses = True
def map_serializer_field(self, auto_schema, direction):
schema = auto_schema._map_serializer_field(self.target, direction, bypass_extensions=True)
if schema is None:
return schema
if direction == 'request' and self.target.nested:
return {
'oneOf': [
build_basic_type(OpenApiTypes.INT),
schema,
]
}
return schema