验证前自动填充参数
Autofill a parameter before validation
请帮我弄清楚当我按下 Swagger 上的“执行”按钮时如何自动填充参数-UI?
我知道如何用requestInterceptor
修改请求中的参数,但我不喜欢这种方法,因为屏幕上显示的参数和实际请求中的参数看起来不同。
截图
openapi.yaml:
openapi: 3.0.1
info:
title: Swagger Petstore
version: 1.0.0
servers:
- url: https://petstore.swagger.io/v2
paths:
/pet/findByTime:
get:
parameters:
- name: time
in: query
description: Should be auto generated when I press Execute button
required: true
schema:
type: string
responses:
200:
description: successful operation
这是我的代码:
// My plugin
const MyWrapActionPlugin = function(system) {
return {
statePlugins: {
spec: {
wrapActions: {
validateParams: (oriAction, system) => (arr) => {
// TODO: Autofill time parameter with the current time
if (system.getState().getIn(['spec', 'meta', 'paths', arr, 'parameters', 'query.timestamp.hash--309052266', 'value'])) {
system.getState().setIn(['spec', 'meta', 'paths', arr, 'parameters', 'query.timestamp.hash--309052266', 'value'], 'test')
}
return oriAction(arr)
}
}
}
}
}
}
window.onload = function() {
const ui = SwaggerUIBundle({
url: "openapi.yaml",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis
],
plugins: [
MyWrapActionPlugin
]
});
window.ui = ui;
};
validateParams
每当我按下执行按钮时都会发生操作,所以我认为这就是逻辑需要的地方。
我的方法是找到带getIn()
的参数并修改它的值,但它不起作用,我不确定这样做是否正确。
我自己想出来了:
const MyWrapActionPlugin = function(system) {
return {
statePlugins: {
spec: {
wrapActions: {
validateParams: (originalAction, system) => (pathMethod) => {
const date = new Date()
const newValue = date.toISOString().slice(0, 19).replace('T', '')
// change parameter value
system.specActions.changeParam(pathMethod, 'timestamp', 'query', newValue, false)
return originalAction(pathMethod)
}
}
}
}
}
}
请帮我弄清楚当我按下 Swagger 上的“执行”按钮时如何自动填充参数-UI?
我知道如何用requestInterceptor
修改请求中的参数,但我不喜欢这种方法,因为屏幕上显示的参数和实际请求中的参数看起来不同。
截图
openapi.yaml:
openapi: 3.0.1
info:
title: Swagger Petstore
version: 1.0.0
servers:
- url: https://petstore.swagger.io/v2
paths:
/pet/findByTime:
get:
parameters:
- name: time
in: query
description: Should be auto generated when I press Execute button
required: true
schema:
type: string
responses:
200:
description: successful operation
这是我的代码:
// My plugin
const MyWrapActionPlugin = function(system) {
return {
statePlugins: {
spec: {
wrapActions: {
validateParams: (oriAction, system) => (arr) => {
// TODO: Autofill time parameter with the current time
if (system.getState().getIn(['spec', 'meta', 'paths', arr, 'parameters', 'query.timestamp.hash--309052266', 'value'])) {
system.getState().setIn(['spec', 'meta', 'paths', arr, 'parameters', 'query.timestamp.hash--309052266', 'value'], 'test')
}
return oriAction(arr)
}
}
}
}
}
}
window.onload = function() {
const ui = SwaggerUIBundle({
url: "openapi.yaml",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis
],
plugins: [
MyWrapActionPlugin
]
});
window.ui = ui;
};
validateParams
每当我按下执行按钮时都会发生操作,所以我认为这就是逻辑需要的地方。
我的方法是找到带getIn()
的参数并修改它的值,但它不起作用,我不确定这样做是否正确。
我自己想出来了:
const MyWrapActionPlugin = function(system) {
return {
statePlugins: {
spec: {
wrapActions: {
validateParams: (originalAction, system) => (pathMethod) => {
const date = new Date()
const newValue = date.toISOString().slice(0, 19).replace('T', '')
// change parameter value
system.specActions.changeParam(pathMethod, 'timestamp', 'query', newValue, false)
return originalAction(pathMethod)
}
}
}
}
}
}