]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✨ Add support for `FrozenSet` in parameters (e.g. query) (#2938)
authorjuntatalor <juntatalor@gmail.com>
Thu, 25 Aug 2022 21:52:53 +0000 (00:52 +0300)
committerGitHub <noreply@github.com>
Thu, 25 Aug 2022 21:52:53 +0000 (23:52 +0200)
Co-authored-by: saborisov <borisov_s@tass.ru>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
fastapi/dependencies/utils.py
tests/main.py
tests/test_application.py
tests/test_query.py

index f6151f6bd8a6a5dac4445ad1bbb15465abb511b8..d781fdb62525ce48ca6782fe7b5c520326fd8deb 100644 (file)
@@ -34,6 +34,7 @@ from pydantic import BaseModel, create_model
 from pydantic.error_wrappers import ErrorWrapper
 from pydantic.errors import MissingError
 from pydantic.fields import (
+    SHAPE_FROZENSET,
     SHAPE_LIST,
     SHAPE_SEQUENCE,
     SHAPE_SET,
@@ -58,6 +59,7 @@ from starlette.websockets import WebSocket
 sequence_shapes = {
     SHAPE_LIST,
     SHAPE_SET,
+    SHAPE_FROZENSET,
     SHAPE_TUPLE,
     SHAPE_SEQUENCE,
     SHAPE_TUPLE_ELLIPSIS,
index f70496db8e1118a8641f5a173b38fdd3b02d0c27..fce6657040bd8c925e25e5f1af1770de69f66901 100644 (file)
@@ -1,5 +1,5 @@
 import http
-from typing import Optional
+from typing import FrozenSet, Optional
 
 from fastapi import FastAPI, Path, Query
 
@@ -192,3 +192,8 @@ def get_query_param_required_type(query: int = Query()):
 @app.get("/enum-status-code", status_code=http.HTTPStatus.CREATED)
 def get_enum_status_code():
     return "foo bar"
+
+
+@app.get("/query/frozenset")
+def get_query_type_frozenset(query: FrozenSet[int] = Query(...)):
+    return ",".join(map(str, sorted(query)))
index d9194c15c7313e3feadc9100d48c720ccdcded0c..b7d72f9ad176cc0a95ba1eeb2360cd33ccf7d25c 100644 (file)
@@ -1090,6 +1090,41 @@ openapi_schema = {
                 "operationId": "get_enum_status_code_enum_status_code_get",
             }
         },
+        "/query/frozenset": {
+            "get": {
+                "summary": "Get Query Type Frozenset",
+                "operationId": "get_query_type_frozenset_query_frozenset_get",
+                "parameters": [
+                    {
+                        "required": True,
+                        "schema": {
+                            "title": "Query",
+                            "uniqueItems": True,
+                            "type": "array",
+                            "items": {"type": "integer"},
+                        },
+                        "name": "query",
+                        "in": "query",
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "Successful Response",
+                        "content": {"application/json": {"schema": {}}},
+                    },
+                    "422": {
+                        "description": "Validation Error",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "$ref": "#/components/schemas/HTTPValidationError"
+                                }
+                            }
+                        },
+                    },
+                },
+            }
+        },
     },
     "components": {
         "schemas": {
index cdbdd1ccdcd15380e8c1c6d5317d76a5481fd655..0c73eb665eda71f5986e2cbbac6c59199fddceb4 100644 (file)
@@ -53,6 +53,7 @@ response_not_valid_int = {
         ("/query/param-required/int", 422, response_missing),
         ("/query/param-required/int?query=50", 200, "foo bar 50"),
         ("/query/param-required/int?query=foo", 422, response_not_valid_int),
+        ("/query/frozenset/?query=1&query=1&query=2", 200, "1,2"),
     ],
 )
 def test_get_path(path, expected_status, expected_response):