Skip to content

Commit

Permalink
Specify required relationship fields in OpenAPI schema (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
arttuperala authored Jul 1, 2023
1 parent 311385e commit 590dbb4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ any parts of the framework not mentioned in the documentation should generally b
```

* `SerializerMethodResourceRelatedField(many=True)` relationship data now includes a meta section.
* Required relationship fields are now marked as required in the OpenAPI schema.

### Fixed

Expand Down
10 changes: 10 additions & 0 deletions example/tests/__snapshots__/test_openapi.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,11 @@
"$ref": "#/components/schemas/reltoone"
}
},
"required": [
"bio",
"entries",
"comments"
],
"type": "object"
},
"type": {
Expand Down Expand Up @@ -801,6 +806,11 @@
"$ref": "#/components/schemas/reltoone"
}
},
"required": [
"bio",
"entries",
"comments"
],
"type": "object"
},
"type": {
Expand Down
18 changes: 18 additions & 0 deletions rest_framework_json_api/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,15 @@ def get_request_body(self, path, method):
): # noqa E501
if "readOnly" in schema:
del item_schema["properties"]["attributes"]["properties"][name]

if "properties" in item_schema and "relationships" in item_schema["properties"]:
# No required relationships for PATCH
if (
method in ["PATCH", "PUT"]
and "required" in item_schema["properties"]["relationships"]
):
del item_schema["properties"]["relationships"]["required"]

return {
"content": {
ct: {
Expand All @@ -653,6 +662,7 @@ def map_serializer(self, serializer):
# TODO: remove attributes, etc. for relationshipView??
required = []
attributes = {}
relationships_required = []
relationships = {}

for field in serializer.fields.values():
Expand All @@ -668,11 +678,15 @@ def map_serializer(self, serializer):
ManySerializerMethodResourceRelatedField,
),
):
if field.required:
relationships_required.append(format_field_name(field.field_name))
relationships[format_field_name(field.field_name)] = {
"$ref": "#/components/schemas/reltomany"
}
continue
if isinstance(field, serializers.RelatedField):
if field.required:
relationships_required.append(format_field_name(field.field_name))
relationships[format_field_name(field.field_name)] = {
"$ref": "#/components/schemas/reltoone"
}
Expand Down Expand Up @@ -727,6 +741,10 @@ def map_serializer(self, serializer):
"type": "object",
"properties": relationships,
}
if relationships_required:
result["properties"]["relationships"][
"required"
] = relationships_required
return result

def _add_async_response(self, operation):
Expand Down

0 comments on commit 590dbb4

Please sign in to comment.