如何在swagger中隐藏参数?

问题描述 投票:0回答:10

有人成功地从生成的文档中隐藏参数吗?我发现了一个问题here,但是在

@ApiParam(access="internal", required=false)
之前使用
@HeaderParam
似乎不起作用。

scala rest swagger
10个回答
28
投票

希望这有帮助。

对于字段

@ApiModelProperty(required = false, hidden = true)
private String hiddenProperty

对于蜜蜂

@ApiIgnore
public class MyApi {}

参数说明

public void getApi(@ApiIgnore String param){}

@ApiModelProperty(hidden="true")
public String paramInsideClass

10
投票

如果您使用

io.swagger.v3
,则应按照此处迁移指南中的说明使用
@Parameter(hidden = true)
https://springdoc.org/migration-from-springfox.html


8
投票

好的,查看单元测试有帮助。首先你需要定义一个过滤器:

import com.wordnik.swagger.core.filter.SwaggerSpecFilter
import com.wordnik.swagger.model.{Parameter, ApiDescription, Operation}
import java.util

class MySwaggerSpecFilter extends SwaggerSpecFilter{
  override def isOperationAllowed(operation: Operation, api: ApiDescription, params: util.Map[String, util.List[String]], cookies: util.Map[String, String], headers: util.Map[String, util.List[String]]): Boolean = true

  override def isParamAllowed(parameter: Parameter, operation: Operation, api: ApiDescription, params: util.Map[String, util.List[String]], cookies: util.Map[String, String], headers: util.Map[String, util.List[String]]): Boolean = {
    if(parameter.paramAccess == Some("internal")) false
    else true
  }
}

然后在

web.xml

中启用它
    <servlet>
        <servlet-name>DefaultJaxrsConfig</servlet-name>
        <servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
        ...
        <init-param>
            <param-name>swagger.filter</param-name>
            <param-value>com.example.MySwaggerSpecFilter</param-value>
        </init-param>
    </servlet>

4
投票

使用 swagger-springmvc (https://github.com/springfox/springfox) 目前无法使用 SwaggerSpecFilter。但它尊重 @ApiIgnore 注释 - 它可以应用于不应出现在生成的元数据中的方法参数。


3
投票

注释

@ApiParam(hidden = true)
为我解决了问题。

def myFunc(@ApiParam(hidden = true) i: Int) = {...}

3
投票

您可以使用以下方式注释您的字段:

@Schema(description = "foo bar.", required = false, hidden = true, example = "bar")
private String fooDtoField

1
投票

sprigfox-swagger2
实现中,有一个注释
@ApiModelProperty
可以执行此操作。

示例:

@ApiModelProperty(required = false, hidden = true)
private String internallyUsedProperty;

0
投票

This答案描述了springfox中使用

.ignoredParameterTypes
@ApiIgnore

的当前解决方案

0
投票

您可以使用 readOnly 和 writeOnly 关键字将特定属性标记为只读或只写。这很有用,例如,当 GET 返回的属性多于 POST 中使用的属性时,您可以在 GET 和 POST 中使用相同的架构,并将额外的属性标记为只读。 readOnly 属性包含在响应中,但不包含在请求中,writeOnly 属性可以在请求中发送,但不包含在响应中。

type: object
properties:
  id:
    # Returned by GET, not used in POST/PUT/PATCH
    type: integer
    readOnly: true
  username:
    type: string
  password:
    # Used in POST/PUT/PATCH, not returned by GET
    type: string
    writeOnly: true

如果 readOnly 或 writeOnly 属性包含在 required 列表中,则 required 仅影响相关范围 - 仅响应或仅请求。也就是说,只读必需属性仅适用于响应,而只读必需属性仅适用于请求。

https://swagger.io/docs/specification/data-models/data-types/


0
投票

对于 Java Microprofile OpenAPI,您可以像这样使用

@Parameter(hidden = true)

import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
...
public Properties getProps(@Parameter(hidden = true) @HeaderParam("X-Access-Key") String accessKey)
© www.soinside.com 2019 - 2024. All rights reserved.