Skip to content

Commit

Permalink
Use 'anyOf' instead of 'oneOf' to fix nested Maybe's
Browse files Browse the repository at this point in the history
  • Loading branch information
stevladimir committed Dec 17, 2023
1 parent 6f4a07a commit 0c8ca8b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Data/OpenApi/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Data.OpenApi.Internal.Schema where
import Prelude ()
import Prelude.Compat

import Control.Lens hiding (allOf)
import Control.Lens hiding (allOf, anyOf)
import Data.Data.Lens (template)

import Control.Applicative ((<|>))
Expand Down Expand Up @@ -626,7 +626,8 @@ instance (Typeable (Fixed a), HasResolution a) => ToSchema (Fixed a) where decla
instance ToSchema a => ToSchema (Maybe a) where
declareNamedSchema _ = do
ref <- declareSchemaRef (Proxy @a)
pure $ unnamed $ mempty & oneOf ?~ [Inline $ mempty & type_ ?~ OpenApiNull, ref]
-- NB: using 'oneOf' goes wrong for nested Maybe's as both subschemas match 'null'.
pure $ unnamed $ mempty & anyOf ?~ [Inline $ mempty & type_ ?~ OpenApiNull, ref]

instance (ToSchema a, ToSchema b) => ToSchema (Either a b) where
-- To match Aeson instance
Expand Down
2 changes: 2 additions & 0 deletions src/Data/OpenApi/Schema/Generator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ schemaGen _ schema
| Just cases <- schema ^. enum_ = elements cases
schemaGen defns schema
| Just variants <- schema ^. oneOf = schemaGen defns =<< elements (dereference defns <$> variants)
schemaGen defns schema
| Just variants <- schema ^. anyOf = schemaGen defns =<< elements (dereference defns <$> variants)
schemaGen defns schema =
case schema ^. type_ of
Nothing ->
Expand Down
4 changes: 2 additions & 2 deletions test/Data/OpenApi/CommonTestTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ personSchemaJSON = [aesonQQ|
"phone": { "type": "integer" },
"email":
{
"oneOf" :
"anyOf" :
[
{ "type" : "null" },
{ "type": "string" }
Expand Down Expand Up @@ -876,7 +876,7 @@ singleMaybeFieldSchemaJSON = [aesonQQ|
{
"singleMaybeField":
{
"oneOf" :
"anyOf" :
[
{ "type" : "null" },
{ "type": "string" }
Expand Down
1 change: 1 addition & 0 deletions test/Data/OpenApi/Schema/ValidationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ spec = do
prop "TL.Text" $ shouldValidate (Proxy :: Proxy TL.Text)
prop "[String]" $ shouldValidate (Proxy :: Proxy [String])
prop "(Maybe [Int])" $ shouldValidate (Proxy :: Proxy (Maybe [Int]))
prop "(Maybe (Maybe Int))" $ shouldValidate (Proxy :: Proxy (Maybe (Maybe Int)))
prop "(IntMap String)" $ shouldValidate (Proxy :: Proxy (IntMap String))
prop "(Set Bool)" $ shouldValidate (Proxy :: Proxy (Set Bool))
prop "(NonEmpty Bool)" $ shouldValidate (Proxy :: Proxy (NonEmpty Bool))
Expand Down

0 comments on commit 0c8ca8b

Please sign in to comment.