如何在 swagger 后端实现新的媒体类型 [英] How to implement new media types in swagger backend

查看:31
本文介绍了如何在 swagger 后端实现新的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个生成应用程序/zip"的 swagger 规范

I have created a swagger specification which produces "application/zip"

/config:
  get:
    produces:
      - application/zip
    responses:
        200: # OK
          description: All config files
          schema:
            type: string
            format: binary

我已经为这个端点实现了处理程序,但我收到了这个错误

I have implemented the handlers for this endpoint but I get this error

http: panic serving 127.0.0.1:20366: applicationZip producer has not yet been implemented

此错误来自此代码

func NewSampleAPI(spec *loads.Document) *SampleAPI {
    return &SampleAPI{
    ...
        ApplicationZipProducer: runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
            return errors.NotImplemented("applicationZip producer has not yet been implemented")
        }),

在调查这个错误之后,我的发现是我们需要实现这样的东西

After investigating this error my findings are that we need to implement something like this

api := operations.NewSampleAPI(swaggerSpec)
api.ApplicationZipProducer = func(w io.Writer, data interface{}) error {
    ...
}

所以我的问题是

  1. 我们应该在这个 Producer 中放什么,为什么需要实现这个,因为没有实现application/json";?

  1. what should we put in this Producer and why is it necessary to implement this because there is no implementation for "application/json" ?

是application/json"吗?Producer是默认实现的,还需要实现其他Producer吗?

Is "application/json" Producer is implemented by default and we need to implement other producers?

注意:我使用的是swagger2.0";规格

Note: I am using swagger "2.0" spec

推荐答案

既然你已经使用过application/zip"作为响应内容,那么您可能已经实现了可能返回 io.ReadCloser 的后端代码.然后你的 Producer 会是这个样子

Since you have used "application/zip" as response content then you might have implemented the backend code which might be returning io.ReadCloser. Then your Producer will look like this

api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
        if w == nil {
            return errors.New("ApplicationZipProducer requires a writer") // early exit
        }

        if data == nil {
            return errors.New("no data given to produce zip from")
        }
        if zp, ok := data.(io.ReadCloser); ok {
            b, err := ioutil.ReadAll(zp)
            if err != nil {
                return fmt.Errorf("application zip producer: %v", err)
            }
            _, err = w.Write(b)
            return nil
        }
        return fmt.Errorf("%v (%T) is not supported by the ApplicationZipProducer, %s", data, data)
    })

这会将数据接口解析为io.ReadCloser并从中读取数据,然后将其填充到io.Writer中

This will parse the data interface into io.ReadCloser and read data from it and then it will fill into the io.Writer

注意:如果您的主要目的只是将文件作为附件发送,那么您应该使用application/octet-stream";并且它的生产者是默认实现的

Note: If your main purpose is just send file as attachment then you should use "application/octet-stream" and its producer is implemented by default

这篇关于如何在 swagger 后端实现新的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆