【问题标题】:Add description to a Servant OpenAPI specification向仆人 OpenAPI 规范添加描述
【发布时间】:2020-12-22 08:45:12
【问题描述】:

想要将描述添加到 OpenAPI 模式、路径、参数...到所有允许描述子元素的 OpenAPI 元素。

module Spec.User where

import           Control.Lens
import           Data.Aeson
import           Data.OpenApi
import           Data.Typeable (Typeable)
import           GHC.Generics
import           Servant

data User =
    User
        { name :: String
        , age :: Int
        }
    deriving (Show, Generic, Typeable)

instance ToJSON User

instance ToSchema User where
    declareNamedSchema proxy = do
        userSchema <- genericDeclareNamedSchema defaultSchemaOptions proxy
        return $ userSchema
            & schema . description ?~ descSchema
            & schema . properties . ix "name" . mapped . description ?~ descName
            & schema . properties . ix "age" . mapped . description ?~ descAge
      where
        descSchema = "This is the description of 'User' schema"
        descName = "This is the description of 'User.name'"
        descAge = "This is the description of 'User.age'"


newtype UserId =
    UserId Integer
    deriving (Show, Generic, Typeable, ToJSON)

instance ToSchema UserId

instance ToParamSchema UserId

type GetUsers = Get '[JSON] [User]
type GetUser  = Capture "user_id" UserId :> Get '[JSON] User
type PostUser = ReqBody '[JSON] User :> Post '[JSON] UserId

-- FIXME Add description per path, parameter, and response

type UserAPI  = GetUsers :<|> GetUser :<|> PostUser

模式的文档有点好,但错误修剪。例如,拼写错误的“姓名”或“年龄”不会导致警告或错误。

但是,目前我更关心的是如何“轻松”和“安全地”将描述添加到路径规范中?例如

paths:
  /users:
    get:
      DESCRIPTION: |
        bla blub ...
      operationId: findUsers
      parameters:
        - name: tags
          in: query
          DESCRIPTION: foo bar ...
          required: false
          style: form
          schema:
            type: array
            items:
              type: string
        - name: limit
          in: query
          DESCRIPTION: baz boo ...
          required: false
          schema:
            type: integer
            format: int32
      responses: ...

【问题讨论】:

    标签: haskell openapi haskell-lens servant


    【解决方案1】:

    您需要servant-openapi3 从 Servant 自动生成 OpenAPI。然后,您可以使用场镜添加额外的注释,例如描述,请参阅here

    【讨论】:

    • 我已经在使用servant-openapi3了。问题不在于如何生成,而在于如何添加内容。
    • 我没有看到您导入了 Servant.OpenAPI。我发布的文档链接不是您要找的吗?
    • 好吧,我还没有发布BSL8.putStrLn $ encode $ toOpenApi (Proxy :: Proxy UserAPI),但它的基础。让我换一种说法。你能在 Servant-OpenAPI3 中编码上面的 OpenAPI sn-p (/users: ...) 包括所有的描述吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2016-12-06
    • 2020-05-29
    • 2020-09-06
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多