OpenAPI:是否可以在 URL 路径中包含 IP 地址(带点的字符串)?

问题描述 投票:0回答:1

使用

Mojolicious::Plugin::OpenAPI
,是否可以在 URL 路径(不是查询参数)中包含 IP 地址?根据 RFC 3986 §2.2
.
不是保留字符。

当我尝试时:

curl 'localhost:3000/api/echo/192.168.1.1'

我收到 http/404

Not Found
错误:

{
    "errors": [
        {
            "message": "Not Found.",
            "path": "/"
        }
    ],
    "status": 404
}

我的期望是:

{
    "ip": "192.168.1.1"
}

这是 Mojolicious 完整应用程序的示例:

./api.yaml

---
openapi: 3.0.3
servers:
  - url: /api
info:
  version: 1
  title: MyAPI
paths:
  /echo/{ip}:
    get:
      x-mojo-to: Example#echo
      parameters:
      - name: ip
        in: path
        required: true
        schema:
          type: string
          format: ipv4
      responses:
        '200':
          description: OK
        '500':
          description: Internal Server Error

./lib/MyApp.pm

package MyApp;
use Mojo::Base 'Mojolicious', -signatures;

sub startup ($self) {
  $self->plugin(OpenAPI => {url=>$self->home->rel_file("api.yaml")});
  $self->plugin(SwaggerUI => {route=>$self->routes()->any('/swagger'),url=>"/api"});
}

1;

./lib/MyApp/Controller/Example.pm

package MyApp::Controller::Example;
use Mojo::Base 'Mojolicious::Controller', -signatures;

sub echo($self) {
  my $app   = $self->openapi->valid_input or return;
  my $input = $app->validation->output;
  my $ip    = $input->{ip};
  $app->render(openapi=>{ip=>$ip});
}

1;
perl url openapi mojolicious
1个回答
0
投票

受到mojo讨论的启发,并由此答案

  • 标准占位符可以匹配除
    .
    /
  • 之外的所有字符
  • Relaxed 占位符可以匹配除
    /
  • 之外的所有字符
  • 通配符占位符可以匹配所有字符

所以我的

api.yaml
看起来像:

---
openapi: 3.0.3
servers:
  - url: /api
info:
  version: 1
  title: MyAPI
paths:
  /echo/{ip}:
    get:
      x-mojo-to: Example#echo
      parameters:
      - name: ip
        x-mojo-placeholder: "#"
        in: path
        required: true
        schema:
          type: string
          format: ipv4
      responses:
        '200':
          description: OK
        '500':
          description: Internal Server Error
© www.soinside.com 2019 - 2024. All rights reserved.