How to hide a parameter in swagger?
Asked Answered
T

10

25

did anyone succeed to hide a parameter from generated documentation? I found an issue here, but using @ApiParam(access="internal", required=false) before @HeaderParam did not seem to work.

Thacker answered 2/4, 2014 at 13:2 Comment(0)
S
28

Hope this helps.

For Fields

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

For Apis

@ApiIgnore
public class MyApi {}

For Parameters

public void getApi(@ApiIgnore String param){}

@ApiModelProperty(hidden="true")
public String paramInsideClass
Spam answered 16/1, 2017 at 13:31 Comment(0)
A
11

If you are using io.swagger.v3, you should use @Parameter(hidden = true) as described on the migration guide here https://springdoc.org/migrating-from-springfox.html

Acquiesce answered 26/11, 2020 at 15:0 Comment(1)
how to pass there ${some.property} value ? developers have to change contract from boolean to string for corresponding annotation property?Fat
T
8

Ok, looking at the unit tests helped. First you need to define a filter:

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
  }
}

And then enable it in 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>
Thacker answered 2/4, 2014 at 14:5 Comment(0)
S
4

With swagger-springmvc (https://github.com/springfox/springfox) at the moment there's no way to use SwaggerSpecFilter. But it respects @ApiIgnore annotation - it can be applied to method parameter which shouldn't appear in generated metadata.

Scup answered 24/3, 2015 at 11:4 Comment(0)
T
3

Annotation @ApiParam(hidden = true) resolved issue for me.

def myFunc(@ApiParam(hidden = true) i: Int) = {...}
Thrift answered 29/3, 2018 at 16:31 Comment(1)
doesnt exist. at least not in 1.5.0Southward
G
3

You could annotate your fields with:

@Schema(description = "foo bar.", required = false, hidden = true, example = "bar")
private String fooDtoField
Glower answered 30/7, 2021 at 7:25 Comment(0)
S
1

In sprigfox-swagger2 implementation there is an annotation @ApiModelProperty that does this.

Example:

@ApiModelProperty(required = false, hidden = true)
private String internallyUsedProperty;
Sosna answered 12/1, 2016 at 15:35 Comment(0)
P
0

This answer describes the current solution in springfox using .ignoredParameterTypes or @ApiIgnore

Pressman answered 19/5, 2016 at 7:31 Comment(0)
H
0

You can use the readOnly and writeOnly keywords to mark specific properties as read-only or write-only. This is useful, for example, when GET returns more properties than used in POST – you can use the same schema in both GET and POST and mark the extra properties as readOnly. readOnly properties are included in responses but not in requests, and writeOnly properties may be sent in requests but not in responses.

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

If a readOnly or writeOnly property is included in the required list, required affects just the relevant scope – responses only or requests only. That is, read-only required properties apply to responses only, and write-only required properties – to requests only.

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

Hicks answered 25/9, 2021 at 9:34 Comment(0)
O
0

In case of Java Microprofile OpenAPI you use @Parameter(hidden = true) like this:

import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
...
public Properties getProps(@Parameter(hidden = true) @HeaderParam("X-Access-Key") String accessKey)
Ommatidium answered 14/3 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.