I want Swagger/OpenAPI host to reflect the incoming request dynamically rather than…
- User
I want Swagger/OpenAPI host to reflect the incoming request dynamically rather than being hardcoded. 14.4.0 version
Assistant
You can make the Swagger/OpenAPI host dynamic by using the
serversarray with a variable for the host. This allows the host to reflect the incoming request:openapi: 3.0.0 info: title: Dynamic Host API version: 14.4.0 servers: - url: {scheme}://{host}{basePath} variables: scheme: default: https enum: - http - https host: default: example.com basePath: default: /apiIn your application code, you'll need to populate these variables based on the incoming request. For example, in Express.js:
app.use('/api-docs', (req, res, next) => { const scheme = req.protocol; const host = req.get('host'); // Modify the OpenAPI spec before serving const openApiSpec = { /* your spec */ }; openApiSpec.servers[0].variables.scheme.default = scheme; openApiSpec.servers[0].variables.host.default = host; req.swaggerDoc = openApiSpec; next(); }, swaggerUi.serve, swaggerUi.setup());
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.