【问题标题】:How do I can hide the schema model in Swagger?如何在 Swagger 中隐藏模式模型?
【发布时间】:2022-02-02 11:08:20
【问题描述】:

对于未答复的请求,我想隐藏此字段(模型架构)。

my swagger

我的请求

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

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

谢谢!

【问题讨论】:

  • 到目前为止你尝试了什么?问题是什么 ?观看导览:stackoverflow.com/tour 以更好地了解 SO 的工作原理。
  • 不确定我是否理解这个问题。我在您的代码和屏幕截图中都没有看到任何“响应”属性。您希望内容是什么样的?

标签: java swagger-ui playframework-2.5


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尽管您没有说,但根据您在 https://github.com/swagger-api/swagger-play/pull/76#issuecomment-224287765 的帖子,我相信您正在使用 Play Framework。我相信目前在大摇大摆的游戏中打破了“无效”结果(参考: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 _ => ()
              }
            }
          }
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      隐藏所有控制器 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-08
        相关资源
        最近更新 更多