Skip to content

Commit

Permalink
Merge pull request #126 from clarkenciel/fix/handle-uuid-as-string
Browse files Browse the repository at this point in the history
Add UUID parameter type
  • Loading branch information
chenjr0719 committed Aug 6, 2019
2 parents a99b98f + df98f95 commit 9dd6c1a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
8 changes: 8 additions & 0 deletions sanic_openapi/doc.py
@@ -1,3 +1,4 @@
import uuid
from collections import defaultdict
from datetime import date, datetime

Expand Down Expand Up @@ -110,6 +111,11 @@ def serialize(self):
return {"type": "array", "items": items, **super().serialize()}


class UUID(Field):
def serialize(self):
return {"type": "string", "format": "uuid", **super().serialize()}


definitions = {}


Expand Down Expand Up @@ -169,6 +175,8 @@ def serialize_schema(schema):
return Date().serialize()
elif schema is datetime:
return DateTime().serialize()
elif schema is uuid.UUID:
return UUID().serialize()
else:
return Object(schema).serialize()

Expand Down
48 changes: 46 additions & 2 deletions tests/test_fields.py
@@ -1,8 +1,8 @@
from datetime import date, datetime

import pytest
from sanic.response import text
from sanic.response import HTTPResponse, text

import pytest
from sanic_openapi import doc


Expand Down Expand Up @@ -248,6 +248,50 @@ def test(request):
}


def test_uuid_field(app):
field = doc.UUID()
assert field.serialize() == {"type": "string", "format": "uuid"}

@app.get("/<id:uuid>")
@doc.response(204, {})
def test(request):
return HTTPResponse(status=204)

_, response = app.test_client.get("/swagger/swagger.json")
assert response.status == 200
assert response.content_type == "application/json"

swagger_json = response.json
path = swagger_json["paths"]["/{id}"]["get"]
assert path["parameters"][0] == {
"in": "path",
"name": "id",
"type": "string",
"format": "uuid",
"required": True,
}

@app.get("/")
@doc.consumes(field, location="formData", required=True)
@doc.response(204, {})
def test(request):
return HTTPResponse(status=204)

_, response = app.test_client.get("/swagger/swagger.json")
assert response.status == 200
assert response.content_type == "application/json"

swagger_json = response.json
path = swagger_json["paths"]["/"]["get"]
assert path["parameters"][0] == {
"in": "formData",
"name": None,
"type": "string",
"format": "uuid",
"required": True,
}


class TestSchema:
pass

Expand Down

0 comments on commit 9dd6c1a

Please sign in to comment.