m_green
m_green

Reputation: 71

Is there a way to render only specific api endpoint in swagger-ui 3.*?

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]
});

Upvotes: 5

Views: 3716

Answers (1)

Ioan M
Ioan M

Reputation: 1207

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

Upvotes: 1

Related Questions