本文介绍了在 openapi v3 中创建通用路径和特定路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 node.js 服务器使用 express-openapi npm 模块.为此,我需要使用 openapi v3 创建一个通用 url,像这样一个/ressources/{action} 它将包括所有类型的动作,除了一些我特别描述为/ressources/action1 和/ressources/action2这里我是如何描述 url 路径中的通用参数的:

I'm using express-openapi npm module for node.js server. I need for that purpose to create a generic url using openapi v3 like this one /ressources/{action} that would include all types of actions expect a few that I described specificly as /ressources/action1 and /ressources/action2 Here how I described the generic parameter in url's path:

    action:
      name: action
      in: path
      required: true
      schema:
        type: string
        not:
          enum: ['action1', 'action2']

具体url单独描述,不带路径参数

The specific urls are described seperatly without path parameter.

问题是,每当我启动服务器并调用例如/ressources/action1 时,它都会调用通用 url.我认为通用操作路径参数枚举存在问题.有人可以帮助找出在这种情况下如何将我的请求与适当的 url 正确匹配吗?

The issue is that whenever I launch the server and call for example /ressources/action1 it calls the generic url. I think there is an issue with the generic action path parameter enumeration. Can someone help figure out how to correctly match my request with the appropriate url in this situation ?

我还尝试枚举所有可能的通用操作,如下所示:

I also tried to enumerate all generic possible actions like this:

    action:
      name: action
      in: path
      required: true
      schema:
        type: string
        enum: ['action3', 'action4', 'action5', 'action6']

但 action1 和 action2 总是匹配通用 url

but action1 and action2 always matches the generic url

推荐答案

两个定义都是正确的,并且根据 OpenAPI 规范:

Both definitions are correct, and according to the OpenAPI Specification:

匹配 URL 时,具体(非模板化)路径将在模板化路径之前匹配.

...

路径模板匹配

假设以下路径,具体定义/pets/mine,如果使用,将首先匹配:

Assuming the following paths, the concrete definition, /pets/mine, will be matched first if used:

/pets/{petId}
/pets/mine

服务器总是选择通用 URL 的事实是一个错误(或未实现的功能?).您应该针对您使用的任何服务器框架提出问题.

The fact that the server always picks up the generic URL is a bug (or unimplemented feature?). You should open an issue with whatever server framework you are using.

这篇关于在 openapi v3 中创建通用路径和特定路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 13:00