如何大摇大摆地重用字符串块
How to reuse a string block in swagger
我正在为 AWS API 网关编写 swagger 文件。我必须使用一段文本来集成每个端点。这是单个端点当前的样子
'/products/{productId}':
get:
tags:
- product
summary: Get detailed information about a product
consumes:
- application/json
produces:
- application/json
parameters:
- name: productId
in: path
required: true
type: string
responses:
'200':
description: 200 response
schema:
type: array
items:
$ref: '#/definitions/product'
'404':
description: product not found
schema:
type: array
items:
$ref: '#/definitions/product'
x-amazon-apigateway-integration:
requestTemplates:
application/json: >
## See
http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
## This template will pass through all parameters including path,
querystring, header, stage variables, and context through to the
integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
#if($foreach.hasNext),#end
#end
},
"context" : {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath"
}
}
uri: >-
arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:87126xxxxxxx:function:lambdatest_v3/invocations
passthroughBehavior: never
responses:
default:
statusCode: '200'
httpMethod: POST
type: aws
关于 x-amazon-apigateway-integration 的部分需要重复,因为它适用于每条路径。我怎么能不每次都写。是否可以有一个字符串定义来至少包含关于 application/json
的部分?
我尝试创建一个字符串定义,但它在 aws import 上不起作用:
definitions:
MyAPI:
type: string
default: >
#Magic
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
#if($foreach.hasNext),#end
#end
},
"context" : {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath"
}
}
然后在路径中:
x-amazon-apigateway-integration:
requestTemplates:
application/json:
$ref: '#/definitions/MyAPI'
uri: >-
arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:8712xxxxxxxx:function:lambdaTest_v3/invocations
passthroughBehavior: never
responses:
default:
statusCode: '200'
httpMethod: POST
type: aws
http://azimi.me/2015/07/16/split-swagger-into-smaller-files.html
If you’re writing a Swagger API spec and it’s becoming too large, you
can split it into multiple files. Swagger supports JSON Reference
(draft) for using remote and local pieces of JSON to build up a
Swagger document.
JSON Reference Overview
JSON Reference uses the special key $ref to define a “reference” to a
piece of JSON. For example following JSON has a reference to
http://example.com/foo.json:
编译拆分 json 个文件
一旦您将 json 文件拆分为多个引用,就可以使用 json-refs resolve
命令将它们全部编译在一起。
json-refs resolve -I relative swagger-boot.json > docs/swagger/swaggerInternal.json
我正在为 AWS API 网关编写 swagger 文件。我必须使用一段文本来集成每个端点。这是单个端点当前的样子
'/products/{productId}':
get:
tags:
- product
summary: Get detailed information about a product
consumes:
- application/json
produces:
- application/json
parameters:
- name: productId
in: path
required: true
type: string
responses:
'200':
description: 200 response
schema:
type: array
items:
$ref: '#/definitions/product'
'404':
description: product not found
schema:
type: array
items:
$ref: '#/definitions/product'
x-amazon-apigateway-integration:
requestTemplates:
application/json: >
## See
http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
## This template will pass through all parameters including path,
querystring, header, stage variables, and context through to the
integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
#if($foreach.hasNext),#end
#end
},
"context" : {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath"
}
}
uri: >-
arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:87126xxxxxxx:function:lambdatest_v3/invocations
passthroughBehavior: never
responses:
default:
statusCode: '200'
httpMethod: POST
type: aws
关于 x-amazon-apigateway-integration 的部分需要重复,因为它适用于每条路径。我怎么能不每次都写。是否可以有一个字符串定义来至少包含关于 application/json
的部分?
我尝试创建一个字符串定义,但它在 aws import 上不起作用:
definitions:
MyAPI:
type: string
default: >
#Magic
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
#if($foreach.hasNext),#end
#end
},
"context" : {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath"
}
}
然后在路径中:
x-amazon-apigateway-integration:
requestTemplates:
application/json:
$ref: '#/definitions/MyAPI'
uri: >-
arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:8712xxxxxxxx:function:lambdaTest_v3/invocations
passthroughBehavior: never
responses:
default:
statusCode: '200'
httpMethod: POST
type: aws
http://azimi.me/2015/07/16/split-swagger-into-smaller-files.html
If you’re writing a Swagger API spec and it’s becoming too large, you can split it into multiple files. Swagger supports JSON Reference (draft) for using remote and local pieces of JSON to build up a Swagger document.
JSON Reference Overview
JSON Reference uses the special key $ref to define a “reference” to a piece of JSON. For example following JSON has a reference to http://example.com/foo.json:
编译拆分 json 个文件
一旦您将 json 文件拆分为多个引用,就可以使用 json-refs resolve
命令将它们全部编译在一起。
json-refs resolve -I relative swagger-boot.json > docs/swagger/swaggerInternal.json