【问题标题】:how to map this array json in swagger ui?如何在 swagger ui 中映射这个数组 json?
【发布时间】:2016-12-08 22:45:33
【问题描述】:

我正在使用 php 开发 swagger ui,我正在尝试像映射 json 下面

{
    "category": {
    "id": 0,
    "name": "string"
  }
}

我从 swagger ui 演示中尝试过,但无法像上面那样获得映射 json,我怎样才能得到这个 json 映射,它的注释是什么? 请帮忙

我的招摇班是

/**
     * @SWG\Definition(@SWG\Xml(name="MyFl"))
     */ 
    class MyFl
    {

        /**
     * @SWG\Property(format="int64")
     * @var int
     */
    public $id;

    /**
     * @SWG\Property(example="doggie")
     * @var string
     */
    public $name;

    /**
     * @SWG\Property(type="Category")
     */
    public $category;

    }

而我的帖子 api 注释是:

/**
     * @SWG\Post(
     *   path="/docs/fl",
     *   tags={"MY"},
     *   summary="Insert fl info",
     *   operationId="FL",
     *   produces={"application/xml","application/json"},
     *   @SWG\Parameter(in="body",name="body",description="Insert fl info",required=true,@SWG\Schema(ref="#/definitions/MyFl")
     *   ),
     *   @SWG\Response(response="default", description="successful operation")
     * )
     */

通过这个我得到的模型模式如下:

但我想要像这样的 json 架构:

 {
        "category": {
        "id": 0,
        "name": "string"
      }
    }

请帮帮我..

【问题讨论】:

  • 您应该发布到目前为止您所尝试的内容。然后,我们可以帮助您找到正确的答案。这样一来,您将比仅给出答案更好地理解答案。

标签: json swagger swagger-ui swagger-php


【解决方案1】:

免责声明:我已经很久没有使用 swagger-php 注释了,所以我不确定这是不是最好的方法。我的专长更多是 JSON Schema,而不是 swagger-php。

您的数据结构实际上是两个对象。第一个是具有一个属性的对象:“类别”。那么“类别”的值是另一个具有两个属性的对象:“id”和“name”。我很确定您需要将它们建模为两个单独的对象。

/**
 * @SWG\Definition(@SWG\Xml(name="MyFl"))
 */ 
class MyFl
{

    /**
     * @SWG\Property(type=Category)  <-- Don't know if this is the right way to reference another schema.  You will have to check the documentation.
     */
    public $category;

}

/**
 * @SWG\Definition(@SWG\Xml(name="Category"))
 */ 
class Category
{

    /**
     * @SWG\Property(format="int64")
     * @var int
     */
    public $id;

    /**
     * @SWG\Property(example="doggie")
     * @var string
     */
    public $name;

}

希望对您有所帮助。

【讨论】:

  • 如果我猜到@SWG\Property(type=Category) 的语法错误(就像我评论的那样),那是有道理的。我认为,如果您找出引用 Category 的正确语法,它可能会起作用。可能有ref 属性吗? @SWG\Property(ref="#/definitions/Category") 之类的东西?
  • 你能提供一些参考资料吗?
  • 请帮我检查这里的问题stackoverflow.com/questions/39228284/…它与相同
猜你喜欢
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 2023-01-02
相关资源
最近更新 更多