Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findOperationMatches fails on default server / #838

Open
joeppeeters opened this issue Nov 21, 2023 · 2 comments
Open

findOperationMatches fails on default server / #838

joeppeeters opened this issue Nov 21, 2023 · 2 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@joeppeeters
Copy link

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?

@erunion
Copy link
Member

erunion commented Dec 3, 2023

This feels like a bug. Do you have an OpenAPI definition you can share that has this issue?

@erunion erunion added bug Something isn't working help wanted Extra attention is needed labels Dec 3, 2023
@joeppeeters
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Development

No branches or pull requests

2 participants