Swagger schemas for arrays, dimap and comment

This commit is contained in:
Tom Sydney Kerckhove 2021-11-02 11:23:34 +01:00
parent d9095e0c02
commit 46f01c6d82
17 changed files with 98 additions and 17 deletions

View File

@ -5,6 +5,8 @@
"title": ""
},
"definitions": {
"Char": {}
"Char": {
"type": "string"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Int": {}
"Int": {
"type": "number",
"description": "Int"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Int16": {}
"Int16": {
"type": "number",
"description": "Int16"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Int32": {}
"Int32": {
"type": "number",
"description": "Int32"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Int64": {}
"Int64": {
"type": "number",
"description": "Int64"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Int8": {}
"Int8": {
"type": "number",
"description": "Int8"
}
}
}

View File

@ -5,6 +5,8 @@
"title": ""
},
"definitions": {
"Text": {}
"Text": {
"type": "string"
}
}
}

View File

@ -5,6 +5,11 @@
"title": ""
},
"definitions": {
"[Text]": {}
"[Text]": {
"items": {
"type": "string"
},
"type": "array"
}
}
}

View File

@ -5,6 +5,8 @@
"title": ""
},
"definitions": {
"[Char]": {}
"[Char]": {
"type": "string"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Word": {}
"Word": {
"type": "number",
"description": "Word"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Word16": {}
"Word16": {
"type": "number",
"description": "Word16"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Word32": {}
"Word32": {
"type": "number",
"description": "Word32"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Word64": {}
"Word64": {
"type": "number",
"description": "Word64"
}
}
}

View File

@ -5,6 +5,9 @@
"title": ""
},
"definitions": {
"Word8": {}
"Word8": {
"type": "number",
"description": "Word8"
}
}
}

View File

@ -31,6 +31,7 @@ library
aeson
, autodocodec
, base >=4.7 && <5
, insert-ordered-containers
, scientific
, swagger2
, text

View File

@ -16,6 +16,7 @@ library:
dependencies:
- aeson
- autodocodec
- insert-ordered-containers
- scientific
- swagger2
- text

View File

@ -1,26 +1,64 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Autodocodec.Swagger where
import Autodocodec
import Control.Monad
import qualified Data.Aeson as JSON
import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
import Data.Proxy
import Data.Scientific
import Data.Swagger
import Data.Swagger.Declare
import Data.Swagger as Swagger
import Data.Swagger.Declare as Swagger
import Data.Text (Text)
import Debug.Trace
declareNamedSchemaViaCodec :: HasCodec value => Proxy value -> Declare (Definitions Schema) NamedSchema
declareNamedSchemaViaCodec proxy = declareNamedSchemaVia codec proxy
declareNamedSchemaVia :: JSONCodec value -> Proxy value -> Declare (Definitions Schema) NamedSchema
declareNamedSchemaVia c Proxy = go c
declareNamedSchemaVia c' Proxy = go c'
where
go :: Codec context input output -> Declare (Definitions Schema) NamedSchema
go = \case
BoolCodec mname -> NamedSchema mname <$> declareSchema (Proxy :: Proxy Bool)
StringCodec mname -> NamedSchema mname <$> declareSchema (Proxy :: Proxy Text)
NumberCodec mname -> NamedSchema mname <$> declareSchema (Proxy :: Proxy Scientific)
ArrayOfCodec mname c -> do
traceM "hello"
itemsSchema <- go c
itemsSchemaRef <- declareNamedSchemaRef itemsSchema
pure $
NamedSchema mname $
mempty
{ _schemaParamSchema =
mempty
{ _paramSchemaType = Just SwaggerArray,
_paramSchemaItems = Just $ SwaggerItemsObject $ _namedSchemaSchema <$> itemsSchemaRef
}
}
ObjectCodec -> declareNamedSchema (Proxy :: Proxy JSON.Object)
MapCodec _ _ c -> go c
CommentCodec t c -> do
NamedSchema mName s <- go c
let desc = case _schemaDescription s of
Nothing -> Just t
Just d -> Just $ t <> "\n" <> d
pure $ NamedSchema mName $ s {_schemaDescription = desc}
_ -> pure $ NamedSchema Nothing mempty -- TODO
declareNamedSchemaRef :: Swagger.NamedSchema -> Declare (Definitions Schema) (Referenced NamedSchema)
declareNamedSchemaRef namedSchema =
fmap (NamedSchema (_namedSchemaName namedSchema))
<$> declareSpecificSchemaRef (_namedSchemaName namedSchema) (_namedSchemaSchema namedSchema)
declareSpecificSchemaRef :: Maybe Text -> Swagger.Schema -> Declare (Definitions Schema) (Referenced Schema)
declareSpecificSchemaRef mName s =
case mName of
Nothing -> pure $ Inline s
Just n -> do
known <- looks (InsOrdHashMap.member n)
when (not known) $ declare $ InsOrdHashMap.singleton n s
pure $ Ref (Reference n)