mirror of
https://github.com/NorfairKing/autodocodec.git
synced 2024-11-30 03:22:52 +03:00
Swagger schemas for arrays, dimap and comment
This commit is contained in:
parent
d9095e0c02
commit
46f01c6d82
@ -5,6 +5,8 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Char": {}
|
||||
"Char": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Int": {}
|
||||
"Int": {
|
||||
"type": "number",
|
||||
"description": "Int"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Int16": {}
|
||||
"Int16": {
|
||||
"type": "number",
|
||||
"description": "Int16"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Int32": {}
|
||||
"Int32": {
|
||||
"type": "number",
|
||||
"description": "Int32"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Int64": {}
|
||||
"Int64": {
|
||||
"type": "number",
|
||||
"description": "Int64"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Int8": {}
|
||||
"Int8": {
|
||||
"type": "number",
|
||||
"description": "Int8"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,8 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Text": {}
|
||||
"Text": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,11 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"[Text]": {}
|
||||
"[Text]": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,8 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"[Char]": {}
|
||||
"[Char]": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Word": {}
|
||||
"Word": {
|
||||
"type": "number",
|
||||
"description": "Word"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Word16": {}
|
||||
"Word16": {
|
||||
"type": "number",
|
||||
"description": "Word16"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Word32": {}
|
||||
"Word32": {
|
||||
"type": "number",
|
||||
"description": "Word32"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Word64": {}
|
||||
"Word64": {
|
||||
"type": "number",
|
||||
"description": "Word64"
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,9 @@
|
||||
"title": ""
|
||||
},
|
||||
"definitions": {
|
||||
"Word8": {}
|
||||
"Word8": {
|
||||
"type": "number",
|
||||
"description": "Word8"
|
||||
}
|
||||
}
|
||||
}
|
@ -31,6 +31,7 @@ library
|
||||
aeson
|
||||
, autodocodec
|
||||
, base >=4.7 && <5
|
||||
, insert-ordered-containers
|
||||
, scientific
|
||||
, swagger2
|
||||
, text
|
||||
|
@ -16,6 +16,7 @@ library:
|
||||
dependencies:
|
||||
- aeson
|
||||
- autodocodec
|
||||
- insert-ordered-containers
|
||||
- scientific
|
||||
- swagger2
|
||||
- text
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user