简体   繁体   中英

How to return a partial swagger spec to use in swagger-ui

Using this swagger-ui example to ask this question, is there a way to request a portion of the swagger spec's JSON instead of the full thing?

For example, this url returns the full swagger spec as JSON for that example. I'm wondering if there's a way to only request the endpoints for the pet resource?

Thanks in advance.

The easiest approach is to use the filter option of Swagger UI to filter the displayed operations by tags :

<!-- index.html -->

<style>
/* Optional - hide the filter box if needed */
.swagger-ui .filter-container {
  display: none
}
</style>

<script>
window.onload = function() {

  // Build a system
  const ui = SwaggerUIBundle({
    url: "http://petstore.swagger.io/v2/swagger.json",
    filter: "pet",  // <------
    ...

</script>


If you specifically want to mutate the actual API definition file (.yaml/.json), you would need to implement server-side logic that would do that.

You can write statePlugin that wraps an action updateSpec, actually, here you can modify your swagger file.

return {
  statePlugins: {
    spec: {
      wrapActions: {
        updateSpec: (oriAction) => {
          return (spec) => {
            const specObj = JSON.parse(spec);
            // filter or do whatever with your swagger spec here
            return oriAction(JSON.stringify(filteredSpec));
          };
        }
      }
    }
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM