5

I have a spring 4 project with springfox-swagger2 2.8.o generating the swagger 2 spec. To consume this spec I am using swagger-ui 3.22.1 in Front-end app. Currently swagger-ui is showing all the endpoints in the spec, how can i only show a specific endpoint in swagger-ui? is there a way to filter the spec for specific path? or can we dynamically get spec only for specific path/endpoint in springfox-swagger2/swagger-ui?

I have tried to get the spec and filter the json on front-end for that specific endpoint. For example extract only '/my-endpoint' from paths object, and extract only the tag related to '/my-endpoint' then create new json based on the extracted path and tag, It does solve the problem partially but the models object has models of all paths, also whenever i have to show documentation for specific endpoint, I have to fetch the whole spec from the server.

const { paths, tags } = swagger2JsonData;

const { ['/my-endpoint']: path } = paths;
const specifiedEndpointData= { '/my-endpoint': { ...path } };

const specifiedEndpointTag= tags.filter(x => x.name === 'my-endpoint-controller-tag');

const newData = { ...swagger2JsonData, paths: specifiedEndpointData, tags: specifiedEndpointTag};

SwaggerUI({
  spec: newData,
  dom_id: '#swagger-ui-container',
  presets: [SwaggerUI.presets.apis]
});
barbsan
  • 3,418
  • 11
  • 21
  • 28
m_green
  • 71
  • 1
  • 3

1 Answers1

1

One option would be to filter the paths with a regex where you configured your Docket:

@Bean
public Docket petApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select() 
        .apis(RequestHandlerSelectors.any()) 
        .paths(PathSelectors.regex("YOURREGEXHERE")) 
        .build()

More info here: https://springfox.github.io/springfox/docs/snapshot/#quick-start-guides

Ioan M
  • 1,105
  • 6
  • 16
  • 1
    that will generate the spec for the specific paths as regex is hard coded, but I want to generate spec for specific path on the fly, without hard coding, just like passing tag or operationId and getting only json related to that tag or operationId. – m_green May 17 '19 at 06:39