Skip to content

Use prefixItems for tuples #3

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Data/OpenApi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module Data.OpenApi (
Schema(..),
NamedSchema(..),
OpenApiItems(..),
OpenApiPrefixItems(..),
Xml(..),
Pattern,
AdditionalProperties(..),
Expand Down
17 changes: 17 additions & 0 deletions src/Data/OpenApi/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ data OpenApiItems where
OpenApiItemsArray :: [Referenced Schema] -> OpenApiItems
deriving (Eq, Show, Typeable, Data)

data OpenApiPrefixItems where
OpenApiPrefixItemsArray :: [Referenced Schema] -> OpenApiPrefixItems
deriving (Eq, Show, Typeable, Data)

data OpenApiType where
OpenApiString :: OpenApiType
OpenApiNumber :: OpenApiType
Expand Down Expand Up @@ -665,6 +669,7 @@ data Schema = Schema
, _schemaType :: Maybe OpenApiType
, _schemaFormat :: Maybe Format
, _schemaItems :: Maybe OpenApiItems
, _schemaPrefixItems :: Maybe OpenApiPrefixItems
, _schemaMaximum :: Maybe Scientific
, _schemaExclusiveMaximum :: Maybe Bool
, _schemaMinimum :: Maybe Scientific
Expand Down Expand Up @@ -1375,6 +1380,14 @@ instance ToJSON OpenApiItems where
]
toJSON (OpenApiItemsArray x) = object [ "items" .= x ]

instance ToJSON OpenApiPrefixItems where
toJSON (OpenApiPrefixItemsArray []) = object
[ "prefixItems" .= object []
, "maxItems" .= (0 :: Int)
, "example" .= Array mempty
]
toJSON (OpenApiPrefixItemsArray x) = object [ "prefixItems" .= x ]

instance ToJSON Components where
toJSON = sopSwaggerGenericToJSON
toEncoding = sopSwaggerGenericToEncoding
Expand Down Expand Up @@ -1530,6 +1543,10 @@ instance FromJSON OpenApiItems where
parseJSON js@(Array _) = OpenApiItemsArray <$> parseJSON js
parseJSON _ = empty

instance FromJSON OpenApiPrefixItems where
parseJSON js@(Array _) = OpenApiPrefixItemsArray <$> parseJSON js
parseJSON _ = empty

instance FromJSON Components where
parseJSON = sopSwaggerGenericParseJSON

Expand Down
10 changes: 8 additions & 2 deletions src/Data/OpenApi/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
-- 2,
-- 3
-- ],
-- "items": {
-- "prefixItems": {
-- "type": "number"
-- },
-- "type": "array"
Expand All @@ -358,7 +358,7 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
-- "Jack",
-- 25
-- ],
-- "items": [
-- "prefixItems": [
-- {
-- "type": "string"
-- },
Expand Down Expand Up @@ -407,6 +407,7 @@ sketchSchema = sketch . toJSON
& items ?~ case ischema of
Just s -> OpenApiItemsObject (Inline s)
_ -> OpenApiItemsArray (map Inline ys)
& prefixItems ?~ OpenApiPrefixItemsArray (map Inline ys)
where
ys = map go (V.toList xs)
allSame = and ((zipWith (==)) ys (tail ys))
Expand Down Expand Up @@ -573,6 +574,7 @@ sketchStrictSchema = go . toJSON
& maxItems ?~ fromIntegral sz
& minItems ?~ fromIntegral sz
& items ?~ OpenApiItemsArray (map (Inline . go) (V.toList xs))
& prefixItems ?~ OpenApiPrefixItemsArray (map (Inline . go) (V.toList xs))
& uniqueItems ?~ allUnique
& enum_ ?~ [js]
where
Expand Down Expand Up @@ -992,6 +994,10 @@ appendItem x Nothing = Just (OpenApiItemsArray [x])
appendItem x (Just (OpenApiItemsArray xs)) = Just (OpenApiItemsArray (xs ++ [x]))
appendItem _ _ = error "GToSchema.appendItem: cannot append to OpenApiItemsObject"

appendPrefixItem :: Referenced Schema -> Maybe OpenApiPrefixItems -> Maybe OpenApiPrefixItems
appendPrefixItem x Nothing = Just (OpenApiPrefixItemsArray [x])
appendPrefixItem x (Just (OpenApiPrefixItemsArray xs)) = Just (OpenApiPrefixItemsArray (xs ++ [x]))

withFieldSchema :: forall proxy s f. (Selector s, GToSchema f) =>
SchemaOptions -> proxy s f -> Bool -> Schema -> Declare (Definitions Schema) Schema
withFieldSchema opts _ isRequiredField schema = do
Expand Down
6 changes: 6 additions & 0 deletions src/Data/OpenApi/Lens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ instance
=> HasItems s (Maybe OpenApiItems) where
items = schema.items

instance
{-# OVERLAPPABLE #-}
HasSchema s Schema
=> HasPrefixItems s (Maybe OpenApiPrefixItems) where
prefixItems = schema.prefixItems

instance
{-# OVERLAPPABLE #-}
HasSchema s Schema
Expand Down
23 changes: 23 additions & 0 deletions src/Data/OpenApi/Optics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ instance
labelOptic = unto (\x -> OpenApiItemsObject x)
{-# INLINE labelOptic #-}

-- OpenApiPrefixItems prisms

instance
( a ~ [Referenced Schema]
, b ~ [Referenced Schema]
) => LabelOptic "_OpenApiPrefixItemsArray"
A_Review
OpenApiPrefixItems
OpenApiPrefixItems
a
b where
labelOptic = unto (\x -> OpenApiPrefixItemsArray x)
{-# INLINE labelOptic #-}

-- =============================================================
-- More helpful instances for easier access to schema properties

Expand Down Expand Up @@ -232,6 +246,15 @@ instance
) => LabelOptic "items" A_Lens NamedSchema NamedSchema a b where
labelOptic = #schema % #items
{-# INLINE labelOptic #-}

-- #prefixItems

instance
( a ~ Maybe OpenApiPrefixItems
, b ~ Maybe OpenApiPrefixItems
) => LabelOptic "prefixItems" A_Lens NamedSchema NamedSchema a b where
labelOptic = #schema % #prefixItems
{-# INLINE labelOptic #-}

-- #maximum

Expand Down
5 changes: 5 additions & 0 deletions src/Data/OpenApi/Schema/Generator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ schemaGen defns schema =
OpenApiItemsArray refs ->
let itemGens = schemaGen defns . dereference defns <$> refs
in fmap (Array . V.fromList) $ sequence itemGens
| Just prefixItems <- schema ^. prefixItems -> do
case prefixItems of
OpenApiPrefixItemsArray refs ->
let itemGens = schemaGen defns . dereference defns <$> refs
in fmap (Array . V.fromList) $ sequence itemGens
Just OpenApiString -> do
size <- getSize
let minLength' = fromMaybe 0 $ fromInteger <$> schema ^. minLength
Expand Down
2 changes: 1 addition & 1 deletion test/Data/OpenApi/CommonTestTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ ispairSchemaJSON :: Value
ispairSchemaJSON = [aesonQQ|
{
"type": "array",
"items":
"prefixItems":
[
{ "type": "integer" },
{ "type": "string" }
Expand Down