Skip to content

Commit

Permalink
Fix validation when nullable is set
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Vandecrème authored and jean-baptiste.gourlet committed Jun 20, 2024
1 parent 9d2b913 commit 00aca83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Data/OpenApi/Internal/Schema/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ validateSchemaType val = withSchema $ \sch ->

_ ->
case (sch ^. type_, val) of
-- Type must be set for nullable to have effect
-- See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#fixed-fields-20
(Just _, Null) | sch ^. nullable == Just True -> valid
(Just OpenApiNull, Null) -> valid
(Just OpenApiBoolean, Bool _) -> valid
(Just OpenApiInteger, Number n) -> validateInteger n
Expand Down
19 changes: 16 additions & 3 deletions test/Data/OpenApi/CommonTestTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module Data.OpenApi.CommonTestTypes where
import Prelude ()
import Prelude.Compat

import Data.Aeson (ToJSON (..), ToJSONKey (..), Value)
import Data.Aeson (ToJSON (..), ToJSONKey (..), Value, genericToJSON)
import Data.Aeson.QQ.Simple
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (toJSONKeyText)
import Data.Char
import Data.Map (Map)
Expand All @@ -17,6 +18,7 @@ import Data.Set (Set)
import qualified Data.Text as Text
import Data.Word
import GHC.Generics
import Test.QuickCheck (Arbitrary (..))

import Data.OpenApi

Expand Down Expand Up @@ -587,10 +589,14 @@ pairwithrefSchemaJSON = [aesonQQ|
-- PairWithNullRef (non-record product data type with nullable ref)
-- ========================================================================
data PairWithNullRef = PairWithNullRef Integer (Maybe Point)
deriving (Generic)
deriving (Show, Generic)

instance ToJSON PairWithNullRef
instance ToSchema PairWithNullRef

instance Arbitrary PairWithNullRef where
arbitrary = PairWithNullRef <$> arbitrary <*> arbitrary

pairwithnullrefSchemaJSON :: Value
pairwithnullrefSchemaJSON = [aesonQQ|
{
Expand Down Expand Up @@ -618,7 +624,14 @@ pairwithnullrefSchemaJSON = [aesonQQ|
data Point = Point
{ pointX :: Double
, pointY :: Double
} deriving (Generic)
} deriving (Show, Generic)

instance ToJSON Point where
toJSON = genericToJSON Aeson.defaultOptions
{ Aeson.fieldLabelModifier = map toLower . drop (length "point") }

instance Arbitrary Point where
arbitrary = Point <$> arbitrary <*> arbitrary

instance ToSchema Point where
declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
Expand Down
2 changes: 2 additions & 0 deletions test/Data/OpenApi/Schema/ValidationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import GHC.Generics
import Data.OpenApi
import Data.OpenApi.Declare
import Data.OpenApi.Aeson.Compat (stringToKey)
import Data.OpenApi.CommonTestTypes (PairWithNullRef)

import Test.Hspec
import Test.Hspec.QuickCheck
Expand Down Expand Up @@ -93,6 +94,7 @@ spec = do
prop "(Int, String, Double, [Int])" $ shouldValidate (Proxy :: Proxy (Int, String, Double, [Int]))
prop "(Int, String, Double, [Int], Int)" $ shouldValidate (Proxy :: Proxy (Int, String, Double, [Int], Int))
prop "(String, Paint)" $ shouldValidate (Proxy :: Proxy (String, Paint))
prop "PairWithNullRef" $ shouldValidate (Proxy :: Proxy PairWithNullRef)
prop "Person" $ shouldValidate (Proxy :: Proxy Person)
prop "Color" $ shouldValidate (Proxy :: Proxy Color)
prop "Paint" $ shouldValidate (Proxy :: Proxy Paint)
Expand Down

0 comments on commit 00aca83

Please sign in to comment.