title: How to exclude drf router path from drf_spectacular SERVERS setting
body: <p>I would like to verify some best practices when using drf_spectacular with a django project.</p>
  <p>I have an API in a django project where all the endpoints use the api/ prefix, i.e. in urls.py, I'm using.</p>
  <pre><code>path('api/', include('myapp_api.urls')),
  </code></pre>
  <p>The myapp_api project'ss urls.py uses the default router, so something like</p>
  <pre><code>router = routers.DefaultRouter()
  router.register(r'assets', views.AssetsViewSet)
  </code></pre>
  <p>By default, this means that the swagger docs will present all the endpoints as something like...</p>
  <pre><code>/api/assets/{}/
  </code></pre>
  <p>instead of</p>
  <pre><code>/assets{}/
  </code></pre>
  <p>At this point, the swagger UI's test calls will work just fine because it will correctly call <code>https://example.com/api/assets{}/</code>. It just looks a bit messy in SwaggerUI so it's not ideal. I'm getting the impression that the <code>/api</code> is superfluous and should be essentially &quot;handled&quot; by the &quot;servers&quot; value.</p>
  <p>A problem arises when I set the OpenAPI server object, which for drf_spectacular is the <code>SERVERS</code> setting, e.g.</p>
  <pre><code>SPECTACULAR_SETTINGS = {
      'SERVERS': [{'url': 'https://example.com/api'}],
  }
  </code></pre>
  <p>This will result in failures with SwaggerUI test calls because they try to sent requests to the following (not the <code>/api/api</code>)</p>
  <pre><code>https://example.com/api/api/assets/{}/
  </code></pre>
  <p>A secondary problem is that the <code>/api/</code> prefix will <em>still</em> appear on all the SwaggerUI listed endpoints.</p>
  <p>I seem to have two options:</p>
  <ol>
  <li>I could go to my urls.py and use <code>path('', include('myapp_api.urls'))</code> instead of <code>path('api/', include('myapp_api.urls'))</code> but that's not desirable because the prefixing seems to make sense in this context.</li>
  <li>I could use a drf_spectacular preprocessing hook and tweak all the endpoints it's generating.</li>
  </ol>
  <p>e.g.</p>
  <pre><code>SPECTACULAR_SETTINGS = {
      'SERVERS': [{'url': 'https://example.com/api'}],
      'PREPROCESSING_HOOKS': ['my_preprocessing_hooks.strip_the_api_prefix']
  }
  
  
  def strip_the_api_prefix(endpoints, **kwargs):
      for i in range(0, len(endpoints)):
          temp = list(endpoints[i])
          if temp[0].startswith('/api'):
              temp[0] = temp[0][4:]
          endpoints[i] = tuple(temp)
      return endpoints
  </code></pre>
  <p>My question is, this all seems like a brittle hack and I'm wondering if I'm missing something. I would like to know if I'm following best practices etc.</p>
  <p>I'm pretty sure I need to be setting at least one <a href="https://swagger.io/docs/specification/api-host-and-base-path/" rel="nofollow noreferrer">OpenAPI server</a> value, so today it's <code>https://example.com/api</code> which could always be the 'bleeding edge', but later there could be a <code>https://api.example.com/v1</code> if there's multiple major versions in the future that I want to maintain. My understanding is that the OpenAPI <code>server</code> value is important for client applications so that there's less need to guess the non-path part of the URL when interacting with an API.</p>
  <p>I'm presuming that it's best practices to <em>not</em> have prefixes for all the endpoints that are documented in the SwaggerUI, but it seems that drf_spectacular is pulling them automatically from the urls.py files, which is why the <code>/api</code> keeps getting added.</p>
  <p>So in short, is there a better way to handle this situation that using a preprocessing hook?</p>