Skip to content

Commit

Permalink
Make items objects instead of arrays for tuples
Browse files Browse the repository at this point in the history
Workaround for biocad#31

If the tuples has homogeneous types, the generated schema is strict.
On the other hand if there are heterogeneous types, the schema is not very strict because the order in which the types must come is not specified.

Also, I had to use anyOf instead of oneOf because for example the int in (Int, Float) matches both Integer and Number.

Finally, special care had to be taken to handle nullables.
  • Loading branch information
Antoine Vandecrème authored and teto committed Feb 3, 2023
1 parent a850131 commit 970ee64
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 76 deletions.
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
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))
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
4 changes: 2 additions & 2 deletions test/Data/OpenApi/Schema/ValidationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ import Test.Hspec.QuickCheck
import Test.QuickCheck
import Test.QuickCheck.Instances ()

shouldValidate :: (ToJSON a, ToSchema a) => Proxy a -> a -> Bool
shouldValidate _ x = validateToJSON x == []
shouldValidate :: (ToJSON a, ToSchema a) => Proxy a -> a -> Property
shouldValidate _ x = validateToJSON x === []

shouldNotValidate :: forall a. ToSchema a => (a -> Value) -> a -> Bool
shouldNotValidate f = not . null . validateJSON defs sch . f
Expand Down
5 changes: 4 additions & 1 deletion test/Data/OpenApi/SchemaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Prelude ()
import Prelude.Compat

import Control.Lens ((^.))
import Data.Aeson (Value)
import Data.Aeson (Value(..))
import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
import Data.Proxy
import Data.Set (Set)
Expand Down Expand Up @@ -68,6 +68,9 @@ spec = do
context "Unit" $ checkToSchema (Proxy :: Proxy Unit) unitSchemaJSON
context "Person" $ checkToSchema (Proxy :: Proxy Person) personSchemaJSON
context "ISPair" $ checkToSchema (Proxy :: Proxy ISPair) ispairSchemaJSON
context "Either String Int" $ checkToSchema (Proxy :: Proxy EitherStringInt) eitherSchemaJSON
context "ISHomogeneousPair" $ checkToSchema (Proxy :: Proxy ISHomogeneousPair) ishomogeneouspairSchemaJSON
context "PairWithRef" $ checkToSchema (Proxy :: Proxy PairWithRef) pairwithrefSchemaJSON
context "Point (fieldLabelModifier)" $ checkToSchema (Proxy :: Proxy Point) pointSchemaJSON
context "Point5 (many field record)" $ do
checkToSchema (Proxy :: Proxy Point5) point5SchemaJSON
Expand Down

0 comments on commit 970ee64

Please sign in to comment.