Struct openapi_lambda_codegen::CodeGenerator
source · pub struct CodeGenerator { /* private fields */ }
Expand description
OpenAPI Lambda code generator.
This code generator is intended to be called from a build.rs
Rust
build script. It emits an
out.rs
file to the directory referenced by the OUT_DIR
environment variable set by Cargo.
This file defines a module named models
containing Rust types for the input parameters and
request/response bodies defined in the OpenAPI definition. It also defines one
module for each call to add_api_lambda
, which defines an
Api
trait with one method for each operation (path + HTTP method) defined in the OpenAPI
definition.
In addition, the generator writes the following files to the out_dir
directory specified in
the call to new
:
openapi-apigw.yaml
- OpenAPI definition annotated withx-amazon-apigateway-integration
extensions to be used by Amazon API Gateway. This file is also modified from the input OpenAPI definition to help adhere to the subset of OpenAPI features supported by Amazon API Gateway. In particular, all references are merged into a single file, anddiscriminator
properties are removed.- One file for each call to
add_api_lambda
named<MODULE_NAME>_handler.rs
, where<MODULE_NAME>
is themod_name
in theApiLambda
passed toadd_api_lambda
. This file contains a placeholder implementation of the correspondingApi
trait. To get started, copy this file intosrc/
, define a corresponding module (<MODULE_NAME>_handler
) insrc/lib.rs
, and replace each instance oftodo!()
in the trait implementation.
Examples
Mono-Lambda
The following invocation in build.rs
uses a single Lambda function to handle all API endpoints:
CodeGenerator::new("openapi.yaml", ".openapi-lambda")
.add_api_lambda(
ApiLambda::new("backend", LambdaArn::cloud_formation("BackendApiFunction.Alias"))
)
.generate();
Multiple Lambda functions
The following invocation in build.rs
uses multiple Lambda functions, each handling a subset of
API endpoints:
CodeGenerator::new("openapi.yaml", ".openapi-lambda")
.add_api_lambda(
ApiLambda::new("pet", LambdaArn::cloud_formation("PetApiFunction.Alias"))
// Only include API endpoints with the `pet` tag.
.with_op_filter(|op| op.tags.iter().any(|tag| tag == "pet"))
)
.add_api_lambda(
ApiLambda::new("store", LambdaArn::cloud_formation("StoreApiFunction.Alias"))
// Only include API endpoints with the `store` tag.
.with_op_filter(|op| op.tags.iter().any(|tag| tag == "store"))
)
.generate();
Implementations§
source§impl CodeGenerator
impl CodeGenerator
sourcepub fn new<P, O>(openapi_path: P, out_dir: O) -> Self
pub fn new<P, O>(openapi_path: P, out_dir: O) -> Self
Construct a new CodeGenerator
.
Arguments
openapi_path
- Input path to OpenAPI definition in YAML formatout_dir
- Output directory path in whichopenapi-apigw.yaml
and one<MODULE_NAME>_handler.rs
file for each call toadd_api_lambda
will be written
sourcepub fn add_api_lambda(self, builder: ApiLambda) -> Self
pub fn add_api_lambda(self, builder: ApiLambda) -> Self
Register an API Lambda function for code generation.
Each call to this method will result in a module being generated that contains an Api
trait
with methods for the corresponding API endpoints. See ApiLambda
for further details.