biocad / servant-openapi3

OpenAPI 3.0 for Servant
BSD 3-Clause "New" or "Revised" License
40 stars 26 forks source link

missing HasOpenApi instance ? #17

Open teto opened 2 years ago

teto commented 2 years ago

when compiling my program I get a no instance error for:

instance (HasOpenApi (Headers '[Header  ResourceMetadata String] NoContent))

-- type ResourceMetadata = "jinko-resource-type"

Here are the versions:

(ins)➜ ghc-pkg list|grep openapi
    openapi3-3.1.0
    servant-openapi3-2.0.1.2

I've tried looking into the code but there seemed to be several abstractions and I gave up. Maybe with some guidance I could try again.

maksbotan commented 2 years ago

Hi @teto,

Are you sure you are using Headers correctly? It should be embedded in a Verb, like this:

type Api = Get '[] (Headers '[Header "jinko-resource-type" String] NoContent)

openApi = toOpenApi (Proxy :: Proxy Api)

For me this example generates the following openapi:

{
  "openapi": "3.0.0",
  "info": {
    "title": "",
    "version":""
  },
  "paths": {
    "/": {
      "get": {
        "responses": {
          "200": {
            "headers": {
              "jinko-resource-type": {"schema": {"type": "string"}}
            },
            "description":""
          }
        }
      }
    }
  },
  "components": {}
}
timbess commented 1 year ago

@maksbotan I'm not the original poster, but I'm seeing this error for my auth endpoints:

type AuthAPI =
  "register" :> ReqBody '[JSON] RegisterBody :> UVerb 'POST '[JSON] '[WithStatus 200 AuthResp, WithStatus 422 String]
    :<|> "login"
      :> ReqBody '[JSON] LoginBody
      :> UVerb
           'POST
           '[JSON]
           '[ WithStatus
                200
                ( Headers
                    '[ Header "Set-Cookie" SetCookie,
                       Header "Set-Cookie" SetCookie
                     ]
                    AuthResp
                ),
              WithStatus 401 String
            ]

Seems like there may be a missing instance around the UVerb instances when using headers.

maksbotan commented 1 year ago

Can you show the precise error message generated with that code?

And why do you have "Set-Cookie" twice in Headers?

timbess commented 1 year ago
app/Main.hs:107:18: error:
    • No instance for (ToSchema
                         (Headers
                            '[Header "Set-Cookie" SetCookie, Header "Set-Cookie" SetCookie]
                            AuthResp))
        arising from a use of ‘toOpenApi’
    • In the expression: toOpenApi authApi
      In an equation for ‘authApiSwagger’:
          authApiSwagger = toOpenApi authApi
    |
107 | authApiSwagger = toOpenApi authApi

I assume it's because acceptLogin sets two cookies? Not sure was mostly following this example: https://github.com/haskell-servant/servant/tree/master/servant-auth