@@ -4,10 +4,11 @@
4module OpenAPI.Checker.Validate.Param
4module OpenAPI.Checker.Validate.Param
5 ( Behave (..)
5 ( Behave (..)
6 , Issue (..)
6 , Issue (..)
9−import Control.Lens hiding (para)
10import Control.Monad
10import Control.Monad
11import Data.Maybe
12import Data.Maybe
12import Data.OpenApi
13import Data.OpenApi
13import Data.Text as T
14import Data.Text as T
@@ -22,13 +23,18 @@ import Text.Pandoc.Builder
22data EncodingStyle = EncodingStyle
23data EncodingStyle = EncodingStyle
23 { style :: Style
24 { style :: Style
24 , explode :: Bool
25 , explode :: Bool
25− , allowReserved :: Maybe Bool
26− -- ^ Nothing when @in@ parameter is not @query@
26+ , -- | Nothing when @in@ parameter is not @query@
27+ allowReserved :: Maybe Bool
27− } deriving (Eq, Ord, Show)
29+ deriving stock (Eq, Ord, Show)
29paramEncoding :: Param -> EncodingStyle
31paramEncoding :: Param -> EncodingStyle
30−paramEncoding p = EncodingStyle
32+paramEncoding p =
31− { style, explode, allowReserved }
36+ , allowReserved
33 style = fromMaybe defaultStyle $ _paramStyle p
39 style = fromMaybe defaultStyle $ _paramStyle p
34 defaultStyle = case _paramIn p of
40 defaultStyle = case _paramIn p of
@@ -49,19 +55,19 @@ tracedSchema par = _paramSchema (extract par) <&> traced (ask par >>> step Param
50instance Issuable 'PathFragmentLevel where
56instance Issuable 'PathFragmentLevel where
51 data Issue 'PathFragmentLevel
57 data Issue 'PathFragmentLevel
53− -- ^ Params have different names
58+ = -- | Params have different names
54− | ParamEmptinessIncompatible
59+ ParamNameMismatch
55− -- ^ Consumer requires non-empty param, but producer gives emptyable
60+ | -- | Consumer requires non-empty param, but producer gives emptyable
56− | ParamRequired
61+ ParamEmptinessIncompatible
57− -- ^ Consumer requires mandatory parm, but producer optional
62+ | -- | Consumer requires mandatory parm, but producer optional
58 | ParamPlaceIncompatible
64 | ParamPlaceIncompatible
60− -- ^ Params encoded in different styles
65+ | -- | Params encoded in different styles
61− | ParamSchemaMismatch
66+ ParamStyleMismatch
62− -- ^ One of schemas not presented
67+ | -- | One of schemas not presented
63 | PathFragmentsDontMatch Text Text
69 | PathFragmentsDontMatch Text Text
64− deriving (Eq, Ord, Show)
70+ deriving stock (Eq, Ord, Show)
65 issueIsUnsupported _ = False
71 issueIsUnsupported _ = False
66 describeIssue ParamNameMismatch = para "The path fragments don't match."
72 describeIssue ParamNameMismatch = para "The path fragments don't match."
67 describeIssue ParamEmptinessIncompatible = para "Expected that an empty parameter is allowed, but it isn't."
73 describeIssue ParamEmptinessIncompatible = para "Expected that an empty parameter is allowed, but it isn't."
@@ -74,8 +80,8 @@ instance Issuable 'PathFragmentLevel where
74instance Behavable 'PathFragmentLevel 'SchemaLevel where
80instance Behavable 'PathFragmentLevel 'SchemaLevel where
75 data Behave 'PathFragmentLevel 'SchemaLevel
81 data Behave 'PathFragmentLevel 'SchemaLevel
76 = InParamSchema
82 = InParamSchema
77− deriving (Eq, Ord, Show)
83+ deriving stock (Eq, Ord, Show)
79 describeBehaviour InParamSchema = "JSON Schema"
85 describeBehaviour InParamSchema = "JSON Schema"
81instance Subtree Param where
87instance Subtree Param where
@@ -92,30 +98,32 @@ instance Subtree Param where
92 structuralEq $ fmap _paramExplode <$> pc
98 structuralEq $ fmap _paramExplode <$> pc
94 checkSemanticCompatibility env beh pc@(ProdCons p c) = do
100 checkSemanticCompatibility env beh pc@(ProdCons p c) = do
95− when (_paramName (extract p) /= _paramName (extract c))
101+ when (_paramName (extract p) /= _paramName (extract c)) $
96− $ issueAt beh ParamNameMismatch
102+ issueAt beh ParamNameMismatch
97− when ((fromMaybe False . _paramRequired . extract $ c) &&
104+ ((fromMaybe False . _paramRequired . extract $ c)
98− not (fromMaybe False . _paramRequired . extract $ p))
105+ && not (fromMaybe False . _paramRequired . extract $ p))
99 $ issueAt beh ParamRequired
106 $ issueAt beh ParamRequired
100 case (_paramIn . extract $ p, _paramIn . extract $ c) of
107 case (_paramIn . extract $ p, _paramIn . extract $ c) of
101 (ParamQuery, ParamQuery) -> do
108 (ParamQuery, ParamQuery) -> do
102 -- Emptiness is only for query params
109 -- Emptiness is only for query params
103− when ((fromMaybe False . _paramAllowEmptyValue . extract $ p)
111+ ((fromMaybe False . _paramAllowEmptyValue . extract $ p)
104− && not (fromMaybe False . _paramAllowEmptyValue . extract $ c))
112+ && not (fromMaybe False . _paramAllowEmptyValue . extract $ c))
105 $ issueAt beh ParamEmptinessIncompatible
113 $ issueAt beh ParamEmptinessIncompatible
106 (a, b) | a == b -> pure ()
114 (a, b) | a == b -> pure ()
107 _ -> issueAt beh ParamPlaceIncompatible
115 _ -> issueAt beh ParamPlaceIncompatible
108− unless (paramEncoding (extract p) == paramEncoding (extract c))
116+ unless (paramEncoding (extract p) == paramEncoding (extract c)) $
109− $ issueAt beh ParamStyleMismatch
117+ issueAt beh ParamStyleMismatch
110 case tracedSchema <$> pc of
118 case tracedSchema <$> pc of
111 ProdCons (Just prodSchema) (Just consSchema) -> do
119 ProdCons (Just prodSchema) (Just consSchema) -> do
112 checkCompatibility env (beh >>> step InParamSchema) $ ProdCons prodSchema consSchema
120 checkCompatibility env (beh >>> step InParamSchema) $ ProdCons prodSchema consSchema
113 ProdCons Nothing Nothing -> pure ()
121 ProdCons Nothing Nothing -> pure ()
114 ProdCons Nothing (Just _consSchema) -> issueAt beh ParamSchemaMismatch
122 ProdCons Nothing (Just _consSchema) -> issueAt beh ParamSchemaMismatch
115 ProdCons (Just _prodSchema) Nothing -> pure ()
123 ProdCons (Just _prodSchema) Nothing -> pure ()
116− -- If consumer doesn't care then why we should?
124+ -- If consumer doesn't care then why we should?
119instance Steppable Param (Referenced Schema) where
127instance Steppable Param (Referenced Schema) where
120 data Step Param (Referenced Schema) = ParamSchema
128 data Step Param (Referenced Schema) = ParamSchema
121− deriving (Eq, Ord, Show)
129+ deriving stock (Eq, Ord, Show)