Skip to content

Commit

Permalink
Exclude callable field defaults from the OpenAPI schema (#1167)
Browse files Browse the repository at this point in the history
* Exclude callable field defaults from the OpenAPI schema

* Organize callable default test in line with project standards
  • Loading branch information
arttuperala authored Jul 13, 2023
1 parent 126cd9f commit b7dd585
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ any parts of the framework not mentioned in the documentation should generally b
* Fixed "id" field being added to /data/attributes in the OpenAPI schema when it is not rendered there.
* Fixed `SerializerMethodResourceRelatedField(many=True)` fields being given
a "reltoone" schema reference instead of "reltomany".
* Callable field default values are excluded from the OpenAPI schema, as they don't resolve to YAML data types.

## [6.0.0] - 2022-09-24

Expand Down
2 changes: 1 addition & 1 deletion rest_framework_json_api/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def map_serializer(self, serializer):
schema["writeOnly"] = True
if field.allow_null:
schema["nullable"] = True
if field.default and field.default != empty:
if field.default and field.default != empty and not callable(field.default):
schema["default"] = field.default
if field.help_text:
# Ensure django gettext_lazy is rendered correctly
Expand Down
11 changes: 11 additions & 0 deletions tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from rest_framework_json_api.schemas.openapi import AutoSchema
from tests.serializers import CallableDefaultSerializer


class TestAutoSchema:
def test_schema_callable_default(self):
inspector = AutoSchema()
result = inspector.map_serializer(CallableDefaultSerializer())
assert result["properties"]["attributes"]["properties"]["field"] == {
"type": "string",
}
19 changes: 13 additions & 6 deletions tests/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework_json_api import serializers
from rest_framework_json_api.relations import ResourceRelatedField
from rest_framework_json_api.serializers import ModelSerializer
from tests.models import (
BasicModel,
ForeignKeySource,
Expand All @@ -9,36 +9,43 @@
)


class BasicModelSerializer(ModelSerializer):
class BasicModelSerializer(serializers.ModelSerializer):
class Meta:
fields = ("text",)
model = BasicModel


class ForeignKeySourceSerializer(ModelSerializer):
class ForeignKeySourceSerializer(serializers.ModelSerializer):
target = ResourceRelatedField(queryset=ForeignKeyTarget.objects)

class Meta:
model = ForeignKeySource
fields = ("target",)


class ManyToManySourceSerializer(ModelSerializer):
class ManyToManySourceSerializer(serializers.ModelSerializer):
targets = ResourceRelatedField(many=True, queryset=ManyToManyTarget.objects)

class Meta:
model = ManyToManySource
fields = ("targets",)


class ManyToManyTargetSerializer(ModelSerializer):
class ManyToManyTargetSerializer(serializers.ModelSerializer):
class Meta:
model = ManyToManyTarget


class ManyToManySourceReadOnlySerializer(ModelSerializer):
class ManyToManySourceReadOnlySerializer(serializers.ModelSerializer):
targets = ResourceRelatedField(many=True, read_only=True)

class Meta:
model = ManyToManySource
fields = ("targets",)


class CallableDefaultSerializer(serializers.Serializer):
field = serializers.CharField(default=serializers.CreateOnlyDefault("default"))

class Meta:
fields = ("field",)

0 comments on commit b7dd585

Please sign in to comment.