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

Make items objects instead of arrays for tuples #2

Merged
merged 2 commits into from
Feb 3, 2023
Merged
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
149 changes: 85 additions & 64 deletions src/Data/OpenApi/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
Expand All @@ -24,7 +25,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.Monad
Expand Down Expand Up @@ -356,14 +357,16 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
-- "Jack",
-- 25
-- ],
-- "items": [
-- {
-- "type": "string"
-- },
-- {
-- "type": "number"
-- }
-- ],
-- "items": {
-- "anyOf": [
-- {
-- "type": "string"
-- },
-- {
-- "type": "number"
-- }
-- ]
-- }
-- "type": "array"
-- }
--
Expand Down Expand Up @@ -404,7 +407,7 @@ sketchSchema = sketch . toJSON
& type_ ?~ OpenApiArray
& items ?~ case ischema of
Just s -> OpenApiItemsObject (Inline s)
_ -> OpenApiItemsArray (map Inline ys)
_ -> OpenApiItemsObject (Inline $ mempty & anyOf ?~ (map Inline ys))
where
ys = map go (V.toList xs)
allSame = and ((zipWith (==)) ys (tail ys))
Expand Down Expand Up @@ -440,35 +443,37 @@ sketchSchema = sketch . toJSON
-- 3
-- ]
-- ],
-- "items": [
-- {
-- "enum": [
-- 1
-- ],
-- "maximum": 1,
-- "minimum": 1,
-- "multipleOf": 1,
-- "type": "number"
-- },
-- {
-- "enum": [
-- 2
-- ],
-- "maximum": 2,
-- "minimum": 2,
-- "multipleOf": 2,
-- "type": "number"
-- },
-- {
-- "enum": [
-- 3
-- ],
-- "maximum": 3,
-- "minimum": 3,
-- "multipleOf": 3,
-- "type": "number"
-- }
-- ],
-- "items": {
-- "anyOf": [
-- {
-- "enum": [
-- 1
-- ],
-- "maximum": 1,
-- "minimum": 1,
-- "multipleOf": 1,
-- "type": "number"
-- },
-- {
-- "enum": [
-- 2
-- ],
-- "maximum": 2,
-- "minimum": 2,
-- "multipleOf": 2,
-- "type": "number"
-- },
-- {
-- "enum": [
-- 3
-- ],
-- "maximum": 3,
-- "minimum": 3,
-- "multipleOf": 3,
-- "type": "number"
-- }
-- ]
-- },
-- "maxItems": 3,
-- "minItems": 3,
-- "type": "array",
Expand All @@ -483,26 +488,28 @@ sketchSchema = sketch . toJSON
-- 25
-- ]
-- ],
-- "items": [
-- {
-- "enum": [
-- "Jack"
-- ],
-- "maxLength": 4,
-- "minLength": 4,
-- "pattern": "Jack",
-- "type": "string"
-- },
-- {
-- "enum": [
-- 25
-- ],
-- "maximum": 25,
-- "minimum": 25,
-- "multipleOf": 25,
-- "type": "number"
-- }
-- ],
-- "items": {
-- "anyOf": [
-- {
-- "enum": [
-- "Jack"
-- ],
-- "maxLength": 4,
-- "minLength": 4,
-- "pattern": "Jack",
-- "type": "string"
-- },
-- {
-- "enum": [
-- 25
-- ],
-- "maximum": 25,
-- "minimum": 25,
-- "multipleOf": 25,
-- "type": "number"
-- }
-- ]
-- },
-- "maxItems": 2,
-- "minItems": 2,
-- "type": "array",
Expand Down Expand Up @@ -982,10 +989,22 @@ gdeclareSchemaRef opts proxy = do
return $ Ref (Reference name)
_ -> Inline <$> gdeclareSchema opts proxy

appendItem :: Referenced Schema -> Maybe OpenApiItems -> Maybe OpenApiItems
appendItem x Nothing = Just (OpenApiItemsArray [x])
appendItem x (Just (OpenApiItemsArray xs)) = Just (OpenApiItemsArray (xs ++ [x]))
appendItem _ _ = error "GToSchema.appendItem: cannot append to OpenApiItemsObject"
addItem :: (Referenced Schema -> [Referenced Schema] -> [Referenced Schema])
-> Referenced Schema
-> Maybe OpenApiItems
-> Maybe OpenApiItems
addItem _ x Nothing = Just (OpenApiItemsArray [x])
addItem add x (Just (OpenApiItemsArray xs)) = case xs of
[] -> Just $ OpenApiItemsObject x
[x'] | x == x' -> Just $ OpenApiItemsObject x
_ | x `elem` xs -> Just $ OpenApiItemsObject $ Inline $ mempty & anyOf ?~ xs
_ -> Just $ OpenApiItemsObject $ Inline $ mempty & anyOf ?~ (add x xs)
addItem add x (Just (OpenApiItemsObject (Inline s))) =
let appendMaybe = Just . maybe [x] (\xs -> if x `elem` xs then xs else add x xs)
in Just $ OpenApiItemsObject $ Inline $ s & anyOf %~ appendMaybe
addItem add x j@(Just (OpenApiItemsObject ref))
| x == ref = j
| otherwise = Just $ OpenApiItemsObject $ Inline $ mempty & anyOf ?~ (add x [ref])

withFieldSchema :: forall proxy s f. (Selector s, GToSchema f) =>
SchemaOptions -> proxy s f -> Bool -> Schema -> Declare (Definitions Schema) Schema
Expand All @@ -995,7 +1014,8 @@ withFieldSchema opts _ isRequiredField schema = do
if T.null fname
then schema
& type_ ?~ OpenApiArray
& items %~ appendItem ref
& items %~ (if isRequiredField then id else addItem (:) nullSchema)
& items %~ addItem (\x xs -> xs ++ [x]) ref
& maxItems %~ Just . maybe 1 (+1) -- increment maxItems
& minItems %~ Just . maybe 1 (+1) -- increment minItems
else schema
Expand All @@ -1005,6 +1025,7 @@ withFieldSchema opts _ isRequiredField schema = do
then required %~ (++ [fname])
else id
where
nullSchema = Inline $ mempty & type_ ?~ OpenApiNull
fname = T.pack (fieldLabelModifier opts (selName (Proxy3 :: Proxy3 s f p)))

-- | Optional record fields.
Expand Down
7 changes: 5 additions & 2 deletions src/Data/OpenApi/Internal/Schema/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import Prelude ()
import Prelude.Compat

import Control.Applicative
import Control.Lens hiding (allOf)
import Control.Lens hiding (allOf, anyOf)
import Control.Monad (forM, forM_, when)

import Data.Aeson hiding (Result)
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.KeyMap as KeyMap
#endif
import Data.Foldable (for_, sequenceA_,
import Data.Foldable (asum, for_, sequenceA_,
traverse_)
#if !MIN_VERSION_aeson(2,0,0)
import Data.HashMap.Strict (HashMap)
Expand Down Expand Up @@ -490,6 +490,9 @@ validateSchemaType val = withSchema $ \sch ->
0 -> invalid $ "Value not valid under any of 'oneOf' schemas: " ++ show val
1 -> valid
_ -> invalid $ "Value matches more than one of 'oneOf' schemas: " ++ show val
(view anyOf -> Just variants) -> do
(asum $ (\var -> validateWithSchemaRef var val) <$> variants)
<|> (invalid $ "Value not valid under any of 'anyOf' schemas: " ++ show val)
(view allOf -> Just variants) -> do
-- Default semantics for Validation Monad will abort when at least one
-- variant does not match.
Expand Down
83 changes: 79 additions & 4 deletions test/Data/OpenApi/CommonTestTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,40 @@ characterInlinedPlayerSchemaJSON = [aesonQQ|
}
|]

-- ========================================================================
-- Either String Int
-- ========================================================================
type EitherStringInt = Either String Int

eitherSchemaJSON :: Value
eitherSchemaJSON = [aesonQQ|
{
"oneOf": [{
"required": ["Left"],
"type": "object",
"properties": {
"Left": {
"type": "string"
}
}
}, {
"required": ["Right"],
"type": "object",
"properties": {
"Right": {
"maximum": 9223372036854775807,
"minimum":-9223372036854775808,
"type":"integer"
}
}
}]
}
|]

-- ========================================================================
-- ISPair (non-record product data type)
-- ========================================================================
data ISPair = ISPair Integer String
data ISPair = ISPair (Integer) (Maybe String)
deriving (Generic)

instance ToSchema ISPair
Expand All @@ -493,11 +523,56 @@ ispairSchemaJSON :: Value
ispairSchemaJSON = [aesonQQ|
{
"type": "array",
"items":
[
"items": {
"anyOf": [
{ "type": "null" },
{ "type": "integer" },
{ "type": "string" }
],
]
},
"minItems": 2,
"maxItems": 2
}
|]

-- ========================================================================
-- ISHomogeneousPair (non-record product data type)
-- ========================================================================
data ISHomogeneousPair = ISHomogeneousPair Integer Integer
deriving (Generic)

instance ToSchema ISHomogeneousPair

ishomogeneouspairSchemaJSON :: Value
ishomogeneouspairSchemaJSON = [aesonQQ|
{
"type": "array",
"items": { "type": "integer" },
"minItems": 2,
"maxItems": 2
}
|]

-- ========================================================================
-- PairWithRef (non-record product data type with ref)
-- ========================================================================
data PairWithRef = PairWithRef Integer Point
deriving (Generic)

instance ToSchema PairWithRef

pairwithrefSchemaJSON :: Value
pairwithrefSchemaJSON = [aesonQQ|
{
"type": "array",
"items": {
"anyOf": [
{ "type": "integer" },
{
"$ref": "#/components/schemas/Point"
}
]
},
"minItems": 2,
"maxItems": 2
}
Expand Down
10 changes: 5 additions & 5 deletions test/Data/OpenApi/Schema/GeneratorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ 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 "(IntMap String)" $ shouldValidate (Proxy :: Proxy (IntMap String))
-- prop "(IntMap String)" $ shouldValidate (Proxy :: Proxy (IntMap String))
prop "(Set Bool)" $ shouldValidate (Proxy :: Proxy (Set Bool))
prop "(NonEmpty Bool)" $ shouldValidate (Proxy :: Proxy (NonEmpty Bool))
prop "(HashSet Bool)" $ shouldValidate (Proxy :: Proxy (HashSet Bool))
prop "(Either Int String)" $ shouldValidate (Proxy :: Proxy (Either Int String))
prop "(Int, String)" $ shouldValidate (Proxy :: Proxy (Int, String))
-- prop "(Int, String)" $ shouldValidate (Proxy :: Proxy (Int, String))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these commented tests expected ? didn't want to update the template schema ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests are checking that for any JSON value generated matching the schema generated by the ToSchema instance of (Int, String), we are able to parse it to (Int, String).

An example might make it clearer, we try to check whether we can parse [1, "hello"] to a (Int, String) which we can.
However, since the only constraints we have is having two elements of type Int or String, we also try [1, 1], ["hello", "bye"] and ["hello", 1] which we cannot parse to (Int, String).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

prop "(Map String Int)" $ shouldValidate (Proxy :: Proxy (Map String Int))
prop "(Map T.Text Int)" $ shouldValidate (Proxy :: Proxy (Map T.Text Int))
prop "(Map TL.Text Bool)" $ shouldValidate (Proxy :: Proxy (Map TL.Text Bool))
prop "(HashMap String Int)" $ shouldValidate (Proxy :: Proxy (HashMap String Int))
prop "(HashMap T.Text Int)" $ shouldValidate (Proxy :: Proxy (HashMap T.Text Int))
prop "(HashMap TL.Text Bool)" $ shouldValidate (Proxy :: Proxy (HashMap TL.Text Bool))
prop "Object" $ shouldValidate (Proxy :: Proxy Object)
prop "(Int, String, Double)" $ shouldValidate (Proxy :: Proxy (Int, String, Double))
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 "(Int, String, Double)" $ shouldValidate (Proxy :: Proxy (Int, String, Double))
-- 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))
describe "Invalid FromJSON validation" $ do
prop "WrongType" $ shouldNotValidate (Proxy :: Proxy WrongType)
prop "MissingRequired" $ shouldNotValidate (Proxy :: Proxy MissingRequired)
Expand Down
Loading