findOperationMatches fails on default server `/`
joeppeeters opened this issue · comments
I'm trying to match an incoming request to an operation based on its request path.
Matching appears to be fine as long as there is a server definition. However, in case of an OAS without a server specification (which defaults to /
according to the spec) the matching appears to be failing.
"servers" : [{"url" : "/api"}],
api.findOperationMatches("http://localhost:3000/api/user/john", "get")
#=> { operation }
but,
"servers": [{"url":"/"}],
api.findOperationMatches("http://localhost:3000/user/john", "get")
#=> undefined
I think both situations should succeed. Is this a bug?
This feels like a bug. Do you have an OpenAPI definition you can share that has this issue?
Sure, although the exact spec doesn't really matter. Operation resolution fails for every spec when the server configured as "/"
petstore1.json
petstore1.json
with server set to /
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"name": "MIT",
"url": "https://opensource.org/licenses/MIT"
}
},
"servers": [
{
"url": "/"
}
],
"paths": {
"/pets": {
"get": {
"summary": "List all pets",
"operationId": "listPets",
"tags": [
"pets"
],
"parameters": [
{
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "A paged array of pets",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pets"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Pets": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
},
"Error": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
}
petstore2.json
petstore2.json
with server set to /v1
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"name": "MIT",
"url": "https://opensource.org/licenses/MIT"
}
},
"servers": [
{
"url": "/v1"
}
],
"paths": {
"/pets": {
"get": {
"summary": "List all pets",
"operationId": "listPets",
"tags": [
"pets"
],
"parameters": [
{
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "A paged array of pets",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pets"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Pets": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
},
"Error": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
}
import * as fs from 'node:fs/promises';
const petstoreJSON1 = await fs.readFile('petstore1.json', { encoding: 'utf8' })
const petstoreJSON2 = await fs.readFile('petstore2.json', { encoding: 'utf8' })
import Oas from 'oas';
const petstore1 = await new Oas(petstoreJSON1);
const petstore2 = await new Oas(petstoreJSON2);
console.log(petstore1.findOperation("http://localhost:3000/pets", "get"))
# outputs undefined
console.log(petstore2.findOperation("http://localhost:3000/v1/pets", "get"))
# outputs operation instance