Skip to content

Commit

Permalink
fix: return original resp when no basemodel found in the resp (#353)
Browse files Browse the repository at this point in the history
Signed-off-by: Keming <kemingy94@gmail.com>
  • Loading branch information
kemingy authored Oct 7, 2023
1 parent 3875f67 commit 05441a6
Show file tree
Hide file tree
Showing 16 changed files with 375 additions and 7 deletions.
18 changes: 18 additions & 0 deletions spectree/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ def is_base_model_instance(value: Any) -> bool:
return is_base_model(type(value))


def is_partial_base_model_instance(instance: Any) -> bool:
"""Check if it's a Pydantic BaseModel instance or [BaseModel]
or {key: BaseModel} instance.
"""
if not instance:
return False
if is_base_model_instance(instance):
return True
if isinstance(instance, dict):
return any(
is_partial_base_model_instance(key) or is_partial_base_model_instance(value)
for key, value in instance.items()
)
if isinstance(instance, list) or isinstance(instance, tuple):
return any(is_partial_base_model_instance(value) for value in instance)
return False


def is_root_model(t: Any) -> bool:
"""Check whether a type is a Pydantic RootModel."""
return is_base_model(t) and ROOT_FIELD in t.__fields__
Expand Down
12 changes: 8 additions & 4 deletions spectree/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Union,
)

from .._pydantic import serialize_model_instance
from .._pydantic import is_partial_base_model_instance, serialize_model_instance
from .._types import JsonType, ModelType, OptionalModelType
from ..config import Configuration
from ..response import Response
Expand Down Expand Up @@ -162,6 +162,7 @@ def validate_response(
skip_validation = True
final_response_payload = serialize_model_instance(response_payload)
else:
# non-BaseModel response or partial BaseModel response
final_response_payload = response_payload

if not skip_validation:
Expand All @@ -170,8 +171,11 @@ def validate_response(
if isinstance(final_response_payload, bytes)
else validation_model.parse_obj
)
final_response_payload = serialize_model_instance(
validator(final_response_payload)
)
validated_instance = validator(final_response_payload)
# in case the response model contains (alias, default_none, unset fields) which
# might not be the what the users want, we only return the validated dict when
# the response contains BaseModel
if is_partial_base_model_instance(final_response_payload):
final_response_payload = serialize_model_instance(validated_instance)

return ResponseValidationResult(payload=final_response_payload)
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@
"title": "JSON",
"type": "object"
},
"OptionalAliasResp.7068f62": {
"properties": {
"limit": {
"title": "Limit",
"type": "integer"
},
"name": {
"title": "Name",
"type": "string"
},
"schema": {
"title": "Schema",
"type": "string"
}
},
"required": [
"schema"
],
"title": "OptionalAliasResp",
"type": "object"
},
"Query.7068f62": {
"properties": {
"order": {
Expand Down Expand Up @@ -415,6 +436,37 @@
"tags": []
}
},
"/api/return_optional_alias": {
"get": {
"description": "",
"operationId": "get__api_return_optional_alias",
"parameters": [],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OptionalAliasResp.7068f62"
}
}
},
"description": "OK"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "on_get <GET>",
"tags": []
}
},
"/api/return_root": {
"get": {
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@
"title": "JSON",
"type": "object"
},
"OptionalAliasResp.7068f62": {
"properties": {
"limit": {
"title": "Limit",
"type": "integer"
},
"name": {
"title": "Name",
"type": "string"
},
"schema": {
"title": "Schema",
"type": "string"
}
},
"required": [
"schema"
],
"title": "OptionalAliasResp",
"type": "object"
},
"Query.7068f62": {
"properties": {
"order": {
Expand Down Expand Up @@ -477,6 +498,37 @@
"tags": []
}
},
"/api/return_optional_alias": {
"get": {
"description": "",
"operationId": "get__api_return_optional_alias",
"parameters": [],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OptionalAliasResp.7068f62"
}
}
},
"description": "OK"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_optional_alias_resp <GET>",
"tags": []
}
},
"/api/return_root": {
"get": {
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@
"title": "JSON",
"type": "object"
},
"OptionalAliasResp.7068f62": {
"properties": {
"limit": {
"title": "Limit",
"type": "integer"
},
"name": {
"title": "Name",
"type": "string"
},
"schema": {
"title": "Schema",
"type": "string"
}
},
"required": [
"schema"
],
"title": "OptionalAliasResp",
"type": "object"
},
"Query.7068f62": {
"properties": {
"order": {
Expand Down Expand Up @@ -477,6 +498,37 @@
"tags": []
}
},
"/api/return_optional_alias": {
"get": {
"description": "",
"operationId": "get__api_return_optional_alias",
"parameters": [],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OptionalAliasResp.7068f62"
}
}
},
"description": "OK"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_optional_alias <GET>",
"tags": []
}
},
"/api/return_root": {
"get": {
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@
"title": "JSON",
"type": "object"
},
"OptionalAliasResp.7068f62": {
"properties": {
"limit": {
"title": "Limit",
"type": "integer"
},
"name": {
"title": "Name",
"type": "string"
},
"schema": {
"title": "Schema",
"type": "string"
}
},
"required": [
"schema"
],
"title": "OptionalAliasResp",
"type": "object"
},
"Query.7068f62": {
"properties": {
"order": {
Expand Down Expand Up @@ -482,6 +503,37 @@
"tags": []
}
},
"/api/return_optional_alias": {
"get": {
"description": "",
"operationId": "get__api_return_optional_alias",
"parameters": [],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OptionalAliasResp.7068f62"
}
}
},
"description": "OK"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "get <GET>",
"tags": []
}
},
"/api/user/{name}": {
"post": {
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@
"title": "JSON",
"type": "object"
},
"OptionalAliasResp.7068f62": {
"properties": {
"limit": {
"title": "Limit",
"type": "integer"
},
"name": {
"title": "Name",
"type": "string"
},
"schema": {
"title": "Schema",
"type": "string"
}
},
"required": [
"schema"
],
"title": "OptionalAliasResp",
"type": "object"
},
"Query.7068f62": {
"properties": {
"order": {
Expand Down Expand Up @@ -378,6 +399,37 @@
"tags": []
}
},
"/api/return_optional_alias": {
"get": {
"description": "",
"operationId": "get__api_return_optional_alias",
"parameters": [],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OptionalAliasResp.7068f62"
}
}
},
"description": "OK"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_optional_alias <GET>",
"tags": []
}
},
"/api/return_root": {
"get": {
"description": "",
Expand Down
Loading

0 comments on commit 05441a6

Please sign in to comment.