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;
所以我的
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