Skip to content

Instantly share code, notes, and snippets.

@brprice
Created November 11, 2021 18:05
Show Gist options
  • Save brprice/796be7b6f9c94355b8e119b3d44aacc2 to your computer and use it in GitHub Desktop.
Save brprice/796be7b6f9c94355b8e119b3d44aacc2 to your computer and use it in GitHub Desktop.
openapi3 bug
-- The generated openapi spec for hetrogenous pairs (and other tuples) is invalid:
-- the "items" property cannot be an array. I don't see how to create a valid
-- spec for hetrogenous pairs at all!
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Lens
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.ByteString.Lazy.Char8 as BSL
import Data.Proxy
import Data.OpenApi
main :: IO ()
main = BSL.putStrLn $ encodePretty $ (mempty :: OpenApi) &
components . schemas .~ [ ("Buggy", toSchema (Proxy :: Proxy (Int, String)))]
{
"components": {
"schemas": {
"Buggy": {
"minItems": 2,
"items": [
{
"maximum": 9223372036854775807,
"minimum": -9223372036854775808,
"type": "integer"
},
{
"type": "string"
}
],
"maxItems": 2,
"type": "array"
}
}
},
"openapi": "3.0.0",
"info": {
"version": "",
"title": ""
},
"paths": {}
}
-- The same problem afflicts homogenous pairs, as they are not treated any differently
-- to hetrogenous ones. However, it is clear how to write a valid openapi spec for them
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Lens
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.ByteString.Lazy.Char8 as BSL
import Data.Proxy
import Data.OpenApi
main :: IO ()
main = BSL.putStrLn $ encodePretty $ (mempty :: OpenApi) &
components . schemas .~ [ ("Buggy", toSchema (Proxy :: Proxy (String, String)))]
{
"components": {
"schemas": {
"Buggy": {
"minItems": 2,
"items": [
{
"type": "string"
},
{
"type": "string"
}
],
"maxItems": 2,
"type": "array"
}
}
},
"openapi": "3.0.0",
"info": {
"version": "",
"title": ""
},
"paths": {}
}
-- The problem with tuples causes knock-on problems elsewhere.
-- One particular place is maps.
-- For "complex" keys, Map k a is encoded as [(k,a)]
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Lens
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.ByteString.Lazy.Char8 as BSL
import Data.Map (Map)
import Data.Proxy
import Data.OpenApi
main :: IO ()
main = BSL.putStrLn $ encodePretty $ (mempty :: OpenApi) &
components . schemas .~ [ ("Buggy", toSchema (Proxy :: Proxy (Map [Int] Bool)))]
{
"components": {
"schemas": {
"Buggy": {
"items": {
"minItems": 2,
"items": [
{
"items": {
"maximum": 9223372036854775807,
"minimum": -9223372036854775808,
"type": "integer"
},
"type": "array"
},
{
"type": "boolean"
}
],
"maxItems": 2,
"type": "array"
},
"type": "array"
}
}
},
"openapi": "3.0.0",
"info": {
"version": "",
"title": ""
},
"paths": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment