Skip to content

Commit

Permalink
Fix request OpenAPI schemas for nested models
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Weiner committed Jan 21, 2025
1 parent d11deb6 commit c535923
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions netbox/core/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,21 @@ def map_serializer_field(self, auto_schema, direction):
return component.ref if component else None
else:
return build_basic_type(OpenApiTypes.INT)


# 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 direction == 'request' and self.target.nested:
return {
'oneOf': [
build_basic_type(OpenApiTypes.NUMBER),
schema,
]
}
return schema

0 comments on commit c535923

Please sign in to comment.