java 如何在Swagger中隐藏模式模型?

34gzjxbg  于 2023-06-04  发布在  Java
关注(0)|答案(3)|浏览(247)

对于未答复的请求,我想隐藏此字段(模型模式)。
my swagger
我的要求

@ApiOperation(value = "Create node")
@ApiImplicitParams({
        @ApiImplicitParam(paramType = "body", required = true)
})
public Result insert()

我不想在@ApiOperation中显示response属性。有可能吗
谢谢!

pod7payv

pod7payv1#

我不知道你在问什么。但是,如果您试图在JSON响应中隐藏模型中的特定字段,请尝试fasterxml的jackson-annotations模块中的JsonIgnore annotation。只需将注解添加到您在Response中试图避免的字段。

yqyhoc1h

yqyhoc1h2#

即使你没有说,我相信基于你在www.example.com上的帖子https://github.com/swagger-api/swagger-play/pull/76#issuecomment-224287765,你正在使用Play Framework。我相信一个“无效”的结果是打破了目前的 Swagger 发挥(参考:https://github.com/swagger-api/swagger-play/issues/89)。我的一种方法是(在Scala中)在@ApiOperation中提供responseReference = "void",并执行以下操作作为我的Scala控制器,以显示Swagger规范,用我的更改覆盖它:

package controllers

import controllers.SwaggerBaseApiController
import io.swagger.models.auth.{ApiKeyAuthDefinition, BasicAuthDefinition, In}
import io.swagger.models.properties.RefProperty
import io.swagger.models.{Info, Response}
import play.api.mvc.Action

import scala.collection.JavaConversions._

object Swagger extends SwaggerBaseApiController {

  def json = Action { implicit request =>
    val swagger = getResourceListing(request.host)
    // We need to modify this if it doesn't contain our security definitions yet, but we have to do it atomically
    // This should be fast enough that this synchronization is not too bad
    swagger.synchronized {
      if (!somethingThreadSafeToShowYouveChangedItAlready) fixSwagger(swagger)
    }
    // We trust that the above code only changes a swagger instance once therefore we don't need to
    // synchronize the json marshalling because it should not change beneath it
    returnValue(request, toJsonString(swagger))
  }

  private[this] def fixSwagger(swagger: io.swagger.models.Swagger): Unit = {
    // Omitted some of my other changes...

    swagger.getPaths.values.foreach { value =>

      value.getOperations.foreach { oper =>
        // Omitted some of my other chabnges

        // Any responses that are void need to be simple void
        oper.getResponses.values.foreach { resp =>
          resp.getSchema() match {
            case schema: RefProperty if schema.get$ref() == "#/definitions/void" => resp.setSchema(null)
            case _ => ()
          }
        }
      }
    }
  }
}
bgtovc5b

bgtovc5b3#

隐藏所有控制器API

@ApiIgnore

隐藏选定属性

@ApiModelProperty(required = false, hidden = true)

示例:可见

@ApiModelProperty(
        access = "public",
        name = "amount",
        example = "123.45",
        value = "the amount - in this example without currency.")
public String getAmount() {
    return amount;
}

示例:隐藏

@ApiModelProperty(
       required = false,
       hidden = true
    )
public String getAmount() {
    return amount;
}

相关问题