我正在尝试为我的Servant项目构建Swagger规范。
我的API定义的一部分包含内部模型对象,这些对象的类型为_id
的Data.Bson.ObjectId
字段
type API = rest
:<|> "signup" :> ReqBody '[JSON] Credentials :> Post '[JSON] Account
:<|> "signin" :> ReqBody '[JSON] Credentials :> Post '[JSON] Session
因此,我需要为ToSchema
和Account
定义Session
,以避免例如添加将具有String
字段而不是ObjectId
的其他层。
data Account = Account
{ _id :: ObjectId
, user :: User
, createdAt :: UTCTime
, updatedAt :: UTCTime
} deriving (Generic, Typeable, Show, Read, Eq, Ord)
instance ToBSON Account
instance FromBSON Account
instance ToJSON Account where
toJSON (Account oid user createdAt updatedAt) =
object [ "_id" .= show oid
, "user" .= toJSON user
, "createdAt" .= iso8601Show createdAt
, "updatedAt" .= iso8601Show updatedAt ]
instance FromJSON Account where
parseJSON (Object o) =
Account <$> o .: "_id"
<*> o .: "user"
<*> o .: "createdAt"
<*> o .: "updatedAt"
instance ToSchema Account where
declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
& mapped.schema.description ?~ "This is an example of a Account"
& mapped.schema.example ?~ toJSON Model_Account.demo
instance ToSchema User where
declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
& mapped.schema.description ?~ "This is an example of a User"
& mapped.schema.example ?~ toJSON Model_User.demo
instance ToSchema Session where
declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
& mapped.schema.description ?~ "This is an example of a Session"
& mapped.schema.example ?~ toJSON Model_Session.demo
我还必须为ToSchema
实现ObjectId
,但我不知道该怎么做。
instance ToSchema ObjectId where
declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
& mapped.schema.description ?~ "This is an example of ObjectId"
& mapped.schema.example ?~ "5e8da2d2393d8b1acd000001"
我收到以下错误
Data.Swagger.Internal.Schema.genericDeclareNamedSchema
:: SchemaOptions
-> Proxy ObjectId -> Declare (Definitions Schema) NamedSchema
Defined in ‘Data.Swagger.Internal.Schema’
Server.hs:210:30: error:
• Could not deduce: Data.Swagger.Internal.TypeShape.GenericHasSimpleShape
ObjectId
"genericDeclareNamedSchemaUnrestricted"
(Data.Swagger.Internal.TypeShape.GenericShape (Rep ObjectId))
arising from a use of ‘genericDeclareNamedSchema’
• In the first argument of ‘(&)’, namely
‘genericDeclareNamedSchema defaultSchemaOptions proxy’
In the first argument of ‘(&)’, namely
‘genericDeclareNamedSchema defaultSchemaOptions proxy
& mapped . schema . description
?~ "This is an example of ObjectId"’
In the expression:
genericDeclareNamedSchema defaultSchemaOptions proxy
& mapped . schema . description ?~ "This is an example of ObjectId"
& mapped . schema . example ?~ "5e8da2d2393d8b1acd000001"
Server.hs:210:30: error:
• No instance for (Generic ObjectId)
arising from a use of ‘genericDeclareNamedSchema’
• In the first argument of ‘(&)’, namely
‘genericDeclareNamedSchema defaultSchemaOptions proxy’
In the first argument of ‘(&)’, namely
‘genericDeclareNamedSchema defaultSchemaOptions proxy
& mapped . schema . description
?~ "This is an example of ObjectId"’
In the expression:
genericDeclareNamedSchema defaultSchemaOptions proxy
& mapped . schema . description ?~ "This is an example of ObjectId"
& mapped . schema . example ?~ "5e8da2d2393d8b1acd000001"
我如何为像ToSchema
这样的自定义类型添加ObjectId
实现,由于它实现了String
/ show
,可以轻松转换为read
?
• No instance for (Generic ObjectId)
顾名思义,genericDeclareNamedSchema
仅适用于具有Generic
实例的数据类型。 Generic
没有实现它,所以您唯一的选择是完全手动声明该模式,有关示例,请参阅ObjectId
的文档。