不支持类型”无效类型”,当使用jwk.Key作为swagger响应类型时。

huangapple go评论109阅读模式
英文:

unsupported type "invalid type" when using jwk.Key as swagger response type

问题

我有以下的端点:

  1. package token
  2. import (
  3. "crypto/rsa"
  4. "github.com/dhis2-sre/im-user/pkg/config"
  5. "github.com/dhis2-sre/im-user/pkg/token/helper"
  6. "github.com/gin-gonic/gin"
  7. "log"
  8. "net/http"
  9. )
  10. func ProvideHandler(config config.Config) Handler {
  11. publicKey, err := config.Authentication.Keys.GetPublicKey()
  12. if err != nil {
  13. log.Fatalln(err)
  14. }
  15. return Handler{
  16. publicKey,
  17. }
  18. }
  19. type Handler struct {
  20. publicKey *rsa.PublicKey
  21. }
  22. // Jwks godoc
  23. // swagger:route GET /jwks Jwks
  24. //
  25. // 返回一个包含公钥的JWKS,用于验证在/signin处分发的JWT
  26. //
  27. // responses:
  28. // 200: Jwks
  29. // 415: Error
  30. // 500: Error
  31. func (h *Handler) Jwks(c *gin.Context) {
  32. jwks, err := helper.CreateJwks(h.publicKey)
  33. if err != nil {
  34. _ = c.Error(err)
  35. return
  36. }
  37. c.JSON(http.StatusOK, jwks)
  38. }

以及以下的swagger响应定义:

  1. package token
  2. import "github.com/lestrrat-go/jwx/jwk"
  3. // swagger:response Jwks
  4. type _ struct {
  5. //in: body
  6. _ jwk.Key
  7. }

但是当我尝试使用以下命令生成规范时:

  1. swagger generate spec -o swagger/swagger.yaml -x swagger/sdk --scan-models

我得到以下错误:

  1. unsupported type "invalid type"

如果我使用interface{}而不是jwk.Key,我可以生成规范而不出错,但显然那不是我想要的类型。

英文:

I have the following endpoint

  1. package token
  2. import (
  3. "crypto/rsa"
  4. "github.com/dhis2-sre/im-user/pkg/config"
  5. "github.com/dhis2-sre/im-user/pkg/token/helper"
  6. "github.com/gin-gonic/gin"
  7. "log"
  8. "net/http"
  9. )
  10. func ProvideHandler(config config.Config) Handler {
  11. publicKey, err := config.Authentication.Keys.GetPublicKey()
  12. if err != nil {
  13. log.Fatalln(err)
  14. }
  15. return Handler{
  16. publicKey,
  17. }
  18. }
  19. type Handler struct {
  20. publicKey *rsa.PublicKey
  21. }
  22. // Jwks godoc
  23. // swagger:route GET /jwks Jwks
  24. //
  25. // Return a JWKS containing the public key which can be used to validate the JWT's dispensed at /signin
  26. //
  27. // responses:
  28. // 200: Jwks
  29. // 415: Error
  30. // 500: Error
  31. func (h *Handler) Jwks(c *gin.Context) {
  32. jwks, err := helper.CreateJwks(h.publicKey)
  33. if err != nil {
  34. _ = c.Error(err)
  35. return
  36. }
  37. c.JSON(http.StatusOK, jwks)
  38. }

Along with the following swagger response definition

  1. package token
  2. import "github.com/lestrrat-go/jwx/jwk"
  3. // swagger:response Jwks
  4. type _ struct {
  5. //in: body
  6. _ jwk.Key
  7. }

But when I try to generate the spec using the below command

  1. swagger generate spec -o swagger/swagger.yaml -x swagger/sdk --scan-models

I get the following error

  1. unsupported type "invalid type"

If I use interface{} rather than jwk.Key I can generate the spec without errors but obviously that's not the type I want

答案1

得分: 0

升级到版本v0.29.0解决了这个问题。

英文:

Upgrading to version v0.29.0 solved the issue

huangapple
  • 本文由 发表于 2022年5月21日 20:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/72329558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定