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

Replace items field from array to object with oneOf #69

Open
wants to merge 1 commit 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
13 changes: 10 additions & 3 deletions src/Data/OpenApi/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,10 @@ instance ToJSON OpenApiItems where
, "maxItems" .= (0 :: Int)
, "example" .= Array mempty
]
toJSON (OpenApiItemsArray x) = object [ "items" .= x ]
toJSON (OpenApiItemsArray x) = object [ "items" .=
toJSON (swaggerMempty { _schemaOneOf = Just x
})
]

instance ToJSON Components where
toJSON = sopSwaggerGenericToJSON
Expand Down Expand Up @@ -1476,8 +1479,12 @@ instance FromJSON Header where
instance FromJSON OpenApiItems where
parseJSON js@(Object obj)
| null obj = pure $ OpenApiItemsArray [] -- Nullary schema.
| otherwise = OpenApiItemsObject <$> parseJSON js
parseJSON js@(Array _) = OpenApiItemsArray <$> parseJSON js
| otherwise = do
refSchema <- parseJSON js
case refSchema of
( Inline Schema { _schemaOneOf = Just schemas })
-> pure $ OpenApiItemsArray schemas
r -> pure $ OpenApiItemsObject r
parseJSON _ = empty

instance FromJSON Components where
Expand Down
120 changes: 63 additions & 57 deletions src/Data/OpenApi/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,16 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
-- "Jack",
-- 25
-- ],
-- "items": [
-- {
-- "type": "string"
-- },
-- {
-- "type": "number"
-- }
-- ],
-- "items": {
-- "oneOf": [
-- {
-- "type": "string"
-- },
-- {
-- "type": "number"
-- }
-- ]
-- },
-- "type": "array"
-- }
--
Expand Down Expand Up @@ -432,35 +434,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": {
-- "oneOf": [
-- {
-- "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 @@ -475,26 +479,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": {
-- "oneOf": [
-- {
-- "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
7 changes: 4 additions & 3 deletions test/Data/OpenApi/CommonTestTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,12 @@ ispairSchemaJSON :: Value
ispairSchemaJSON = [aesonQQ|
{
"type": "array",
"items":
[
"items": {
"oneOf": [
{ "type": "integer" },
{ "type": "string" }
],
]
},
"minItems": 2,
"maxItems": 2
}
Expand Down