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

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

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

问题

我有以下的端点:

package token

import (
	"crypto/rsa"
	"github.com/dhis2-sre/im-user/pkg/config"
	"github.com/dhis2-sre/im-user/pkg/token/helper"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func ProvideHandler(config config.Config) Handler {
	publicKey, err := config.Authentication.Keys.GetPublicKey()
	if err != nil {
		log.Fatalln(err)
	}

	return Handler{
		publicKey,
	}
}

type Handler struct {
	publicKey *rsa.PublicKey
}

// Jwks godoc
// swagger:route GET /jwks Jwks
//
// 返回一个包含公钥的JWKS,用于验证在/signin处分发的JWT
//
// responses:
//   200: Jwks
//   415: Error
//   500: Error
func (h *Handler) Jwks(c *gin.Context) {
	jwks, err := helper.CreateJwks(h.publicKey)
	if err != nil {
		_ = c.Error(err)
		return
	}

	c.JSON(http.StatusOK, jwks)
}

以及以下的swagger响应定义:

package token

import "github.com/lestrrat-go/jwx/jwk"

// swagger:response Jwks
type _ struct {
	//in: body
	_ jwk.Key
}

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

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

我得到以下错误:

unsupported type "invalid type"

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

英文:

I have the following endpoint

package token

import (
	"crypto/rsa"
	"github.com/dhis2-sre/im-user/pkg/config"
	"github.com/dhis2-sre/im-user/pkg/token/helper"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func ProvideHandler(config config.Config) Handler {
	publicKey, err := config.Authentication.Keys.GetPublicKey()
	if err != nil {
		log.Fatalln(err)
	}

	return Handler{
		publicKey,
	}
}

type Handler struct {
	publicKey *rsa.PublicKey
}

// Jwks godoc
// swagger:route GET /jwks Jwks
//
// Return a JWKS containing the public key which can be used to validate the JWT's dispensed at /signin
//
// responses:
//   200: Jwks
//   415: Error
//   500: Error
func (h *Handler) Jwks(c *gin.Context) {
	jwks, err := helper.CreateJwks(h.publicKey)
	if err != nil {
		_ = c.Error(err)
		return
	}

	c.JSON(http.StatusOK, jwks)
}

Along with the following swagger response definition

package token

import "github.com/lestrrat-go/jwx/jwk"

// swagger:response Jwks
type _ struct {
	//in: body
	_ jwk.Key
}

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

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

I get the following error

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:

确定