mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Fix type mismatches in API view (#2429)
* Fix tags field to be shown as array in API view `tags` field in serializers is defineded as `TagListSerializerField`. It should be shown as an array value in API view but actually, it is a simple string value. This fixes it by introducing a new `FieldInspector` to handle `TagListSerializerField` type field as an array. It doesn't affects any other type fields. * Fix SerializedPKRelatedField type API expression A field definded as `SerializedPKRelatedField` should be shown as an array of child serializer objects in a response value definition in API view but it is shown as an array of primary key values (usually `integer` type) of a child serializer. This fixes it by introducing a new `FieldInspector` to handle the field. It doesn't affect any other type fields. * Fix request parameter representation in API view In API view, representation of a parameter defined as a sub class of `WritableNestedSerializer` should be vary between a request and a response. For example, `tenant` field in `IPAddressSerializer` should be shown like following as a request body: ``` tenant: integer ... ``` while it should be shown like following as a response body: ``` tenant: { id: integer ..., url: string ..., name: string ..., slug: string ... } ``` But in both cases, it is shown as a response body type expression. This causes an error at sending an API request with that type value. It is only an API view issue, API can handle a request if a request parameter is structured as an expected request body by ignoring the wrong expression. This fixes the issue by replacing an implicitly used default auto schema generator class by its sub class and returning a pseudo serializer with 'Writable' prefix at generating a request body. The reason to introduce a new generator class is that there is no other point which can distinguish a request and a response. It is not enough to distinguish POST, PUT, PATCH methods from GET because former cases may return a JSON object as a response but it is also represented as same as a request body, causes another mismatch. This also fixes `SerializedPKRelatedField` type field representation. It should be shown as an array of primary keys in a request body. Fixed #2400
This commit is contained in:
Tatsushi Demachi
committed by
Jeremy Stretch
parent
d2744700c6
commit
c3cdf8e97e
@ -73,6 +73,7 @@ v2.4.5 (2018-10-02)
|
||||
|
||||
## Bug Fixes
|
||||
|
||||
* [#2400](https://github.com/digitalocean/netbox/issues/2400) - API variable type mismatch at creating/modifying an entry
|
||||
* [#2406](https://github.com/digitalocean/netbox/issues/2406) - Remove hard-coded limit of 1000 objects from API-populated form fields
|
||||
* [#2414](https://github.com/digitalocean/netbox/issues/2414) - Tags field missing from device/VM component creation forms
|
||||
* [#2442](https://github.com/digitalocean/netbox/issues/2442) - Nullify "next" link in API when limit=0 is passed
|
||||
|
@ -275,9 +275,12 @@ RQ_QUEUES = {
|
||||
|
||||
# drf_yasg settings for Swagger
|
||||
SWAGGER_SETTINGS = {
|
||||
'DEFAULT_AUTO_SCHEMA_CLASS': 'utilities.custom_inspectors.NetBoxSwaggerAutoSchema',
|
||||
'DEFAULT_FIELD_INSPECTORS': [
|
||||
'utilities.custom_inspectors.NullableBooleanFieldInspector',
|
||||
'utilities.custom_inspectors.CustomChoiceFieldInspector',
|
||||
'utilities.custom_inspectors.TagListFieldInspector',
|
||||
'utilities.custom_inspectors.SerializedPKRelatedFieldInspector',
|
||||
'drf_yasg.inspectors.CamelCaseJSONFilter',
|
||||
'drf_yasg.inspectors.ReferencingSerializerInspector',
|
||||
'drf_yasg.inspectors.RelatedFieldInspector',
|
||||
|
@ -1,9 +1,52 @@
|
||||
from drf_yasg import openapi
|
||||
from drf_yasg.inspectors import FieldInspector, NotHandled, PaginatorInspector, FilterInspector
|
||||
from drf_yasg.inspectors import FieldInspector, NotHandled, PaginatorInspector, FilterInspector, SwaggerAutoSchema
|
||||
from rest_framework.fields import ChoiceField
|
||||
from rest_framework.relations import ManyRelatedField
|
||||
from taggit_serializer.serializers import TagListSerializerField
|
||||
|
||||
from extras.api.customfields import CustomFieldsSerializer
|
||||
from utilities.api import ChoiceField
|
||||
from utilities.api import ChoiceField, SerializedPKRelatedField, WritableNestedSerializer
|
||||
|
||||
|
||||
class NetBoxSwaggerAutoSchema(SwaggerAutoSchema):
|
||||
def get_request_serializer(self):
|
||||
serializer = super().get_request_serializer()
|
||||
|
||||
if serializer is not None and self.method in self.implicit_body_methods:
|
||||
properties = {}
|
||||
for child_name, child in serializer.fields.items():
|
||||
if isinstance(child, (ChoiceField, WritableNestedSerializer)):
|
||||
properties[child_name] = None
|
||||
elif isinstance(child, ManyRelatedField) and isinstance(child.child_relation, SerializedPKRelatedField):
|
||||
properties[child_name] = None
|
||||
|
||||
if properties:
|
||||
writable_class = type('Writable' + type(serializer).__name__, (type(serializer),), properties)
|
||||
serializer = writable_class()
|
||||
|
||||
return serializer
|
||||
|
||||
|
||||
class SerializedPKRelatedFieldInspector(FieldInspector):
|
||||
def field_to_swagger_object(self, field, swagger_object_type, use_references, **kwargs):
|
||||
SwaggerType, ChildSwaggerType = self._get_partial_types(field, swagger_object_type, use_references, **kwargs)
|
||||
if isinstance(field, SerializedPKRelatedField):
|
||||
return self.probe_field_inspectors(field.serializer(), ChildSwaggerType, use_references)
|
||||
|
||||
return NotHandled
|
||||
|
||||
|
||||
class TagListFieldInspector(FieldInspector):
|
||||
def field_to_swagger_object(self, field, swagger_object_type, use_references, **kwargs):
|
||||
SwaggerType, ChildSwaggerType = self._get_partial_types(field, swagger_object_type, use_references, **kwargs)
|
||||
if isinstance(field, TagListSerializerField):
|
||||
child_schema = self.probe_field_inspectors(field.child, ChildSwaggerType, use_references)
|
||||
return SwaggerType(
|
||||
type=openapi.TYPE_ARRAY,
|
||||
items=child_schema,
|
||||
)
|
||||
|
||||
return NotHandled
|
||||
|
||||
|
||||
class CustomChoiceFieldInspector(FieldInspector):
|
||||
|
Reference in New Issue
Block a user