Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the required flag to request bodies, matching servant semantics #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Servant/OpenApi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ import Servant.OpenApi.Internal.Orphans ()
-- "$ref": "#/components/schemas/User"
-- }
-- }
-- }
-- },
-- "required": true
-- },
-- "responses": {
-- "200": {
Expand Down Expand Up @@ -288,7 +289,8 @@ import Servant.OpenApi.Internal.Orphans ()
-- "$ref": "#/components/schemas/User"
-- }
-- }
-- }
-- },
-- "required": true
-- },
-- "responses": {
-- "200": {
Expand Down Expand Up @@ -420,7 +422,8 @@ import Servant.OpenApi.Internal.Orphans ()
-- "$ref": "#/components/schemas/User"
-- }
-- }
-- }
-- },
-- "required": true
-- },
-- "responses": {
-- "200": {
Expand Down Expand Up @@ -512,7 +515,7 @@ import Servant.OpenApi.Internal.Orphans ()
-- UserId...
-- ...
-- Finished in ... seconds
-- ...3 examples, 0 failures...
-- 3 examples, 0 failures
--
-- Although servant is great, chances are that your API clients don't use Haskell.
-- In many cases @swagger.json@ serves as a specification, not a Haskell type.
Expand Down
2 changes: 2 additions & 0 deletions src/Servant/OpenApi/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ instance (ToSchema a, AllAccept cs, HasOpenApi sub, KnownSymbol (FoldDescription
(defs, ref) = runDeclare (declareSchemaRef (Proxy :: Proxy a)) mempty
reqBody = (mempty :: RequestBody)
& description .~ transDesc (reflectDescription (Proxy :: Proxy mods))
-- ReqBody' is always required, as per the Servant documentation
& required ?~ True
& content .~ InsOrdHashMap.fromList [(t, mempty & schema ?~ ref) | t <- allContentType (Proxy :: Proxy cs)]

-- | This instance is an approximation.
Expand Down
4 changes: 2 additions & 2 deletions src/Servant/OpenApi/Internal/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import Servant.OpenApi.Internal.TypeLevel
-- UserId...
-- ...
-- Finished in ... seconds
-- ...2 examples, 0 failures...
-- 2 examples, 0 failures
--
-- For the test to compile all body types should have the following instances:
--
Expand Down Expand Up @@ -122,7 +122,7 @@ validateEveryToJSONWithPatternChecker checker _ = props
-- [Char]...
-- ...
-- Finished in ... seconds
-- ...3 examples, 0 failures...
-- 3 examples, 0 failures
props :: forall p p'' cs xs. TMap (Every (Typeable ': Show ': Arbitrary ': cs)) xs =>
p cs -- ^ A list of constraints.
-> (forall x. EveryTF cs x => x -> Property) -- ^ Property predicate.
Expand Down
41 changes: 41 additions & 0 deletions test/Servant/OpenApiSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ newtype TodoId = TodoId String deriving (Generic)
instance ToParamSchema TodoId

type TodoAPI = "todo" :> Capture "id" TodoId :> Get '[JSON] Todo
:<|> "todo" :> Capture "id" TodoId :> ReqBody '[JSON] Todo :> Post '[JSON] Todo

todoAPI :: Value
todoAPI = [aesonQQ|
Expand Down Expand Up @@ -129,6 +130,46 @@ todoAPI = [aesonQQ|
"name": "id"
}
]
},
"post": {
"requestBody": {
"required": true,
"content": {
"application/json;charset=utf-8": {
"schema": {
"$ref": "#/components/schemas/Todo"
}
}
}
},
"responses": {
"404": {
"description": "`id` not found"
},
"400": {
"description": "Invalid `body`"
},
"200": {
"content": {
"application/json;charset=utf-8": {
"schema": {
"$ref": "#/components/schemas/Todo"
}
}
},
"description": ""
}
},
"parameters": [
{
"required": true,
"schema": {
"type": "string"
},
"in": "path",
"name": "id"
}
]
}
}
}
Expand Down