LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

OpenAPI Lambda Generator

This generator is based on the OpenAPI 3.0 specification to generate one AWS Lamdbd function per endpoint. Also, it generates all the artifacts for the building and deploying with cloud formation YAML files.

Input

Model

In light-rest-4j framework generator, the model that drives code generation is the OpenAPI 3.0 specification previously named Swagger specification. When editing it, it usually will be in YAML format with separate files for readability and flexibility. Before leveraging it in the light-rest-4j framework, all YAML files need to be bundled to a single file in YAML or JSON format to be consumed by the framework and generator. Also, validation needs to be done to make sure that the openapi.yaml or openapi.json is valid against JSON schema of the OpenAPI 3.0 specification.

Note: currently, we support only the OpenAPI 3.0 specification and Swagger 2.0 specification support was removed.

  • Swagger Editor

  • Swagger CLI

Config

Field NameDescription
projectNameUsed in generated prom.xml for project name Optionality: mandatory
versionUsed in generated pom.xml for project versionOptionality: mandatory
groupIdUsed in generated pom.xml for project groupId Optionality: mandatory
artifactIdused in generated pom.xml for project artifactId Optionality: mandatory
rootPackageroot package name for your project and it will normally be your domain plug project name. Optionality: optional
handlerPackagethe Java package for all generated handlers. Optionality: mandatory
modelPackagethe Java package for all generated models or POJOs. Optionality: mandatory
overwriteHandlercontrols if you want to overwrite handler when regenerating the same project into the same folder. Optionality: mandatory Recommendation: set to false if you only want to upgrade the framework to another minor version and don’t want to overwrite handlers
overwriteHandlerTestcontrols if you want to overwrite handler test cases. Optionality: mandatory
overwriteModelcontrols if you want to overwrite generated models. Optionality: mandatory Recommendation: set to false to prevent the model classes being overwritten
generateModelOnlycontrols whether you wish to generate only the model classes Optionality: optional Recommendation: to be used by teams consuming an API and who wish to generate the model classes only Default: false
useLightProxyreplace the AWS API Gateway with light-proxy for cross-cutting concerns if it is trueOptionality: optional Default: false
launchTypespecify if EC2 or Fargate will be used to launch the light-proxy container. Optionality: mandatory
publicVpcuse public subnet for the VPC to deploy the light-proxy if it is true. Optionality: optional Default: true
packageDockerpackage the Lambda function with Docker image instead of zip file for deployment if it true. Optionality: mandatory Recommendation: false
regionspecify the AWS region for the Lambda fuctions and/or light-proxy deployment. Optionality: mandatory Example: us-east-2
enableRegistrycontrol if built-in service registry/discovery is used Optionality: mandatory Note: Only necessary if running as standalone java -jar xxx
prometheusMetricsdecides whether to wire the Prometheus metrics collection handler in the handler chain. Set to true to skip the wiring of the Prometheus metrics collection handler Optionality: optional Default: false

Here is an example of config.json for openapi generator.

{
  "projectName": "petstore",
  "version": "1.0.1",
  "groupId": "com.networknt",
  "artifactId": "petstore",
  "rootPackage": "com.networknt.petstore",
  "handlerPackage":"com.networknt.petstore.handler",
  "modelPackage":"com.networknt.petstore.model",
  "useLightProxy": true,
  "launchType": "EC2",
  "publicVpc": true,
  "packageDocker": false,
  "region": "us-east-2",
  "overwriteHandler": true,
  "overwriteHandlerTest": true,
  "overwriteModel": true,
  "enableRegistry": false,
  "generateModelOnly": false
}

Given we have most of our model and config files in model-config repo, most generator input would come from the lambda folder in model-config for the light-aws-lambda framework.

Let’s clone the project to your workspace as we will need it in the following steps. I am using ~/networknt as a workspace, but it can be anywhere in your home directory.

cd ~/networknt
git clone https://github.com/networknt/model-config.git

Usage

Java Command line

You can download or build the codegen-cli command line jar.

  • JSON Model

Given we have test openapi.json and config.json in light-rest-4j/src/test/resources folder, the following command line will generate a RESTful API at /tmp/openapi-lambda folder.

Working directory: light-codegen

cd ~/networknt/light-codegen
java -jar codegen-cli/target/codegen-cli.jar -f openapilambda -o /tmp/openapi-lambda -m light-rest-4j/src/test/resources/openapi.json -c light-rest-4j/src/test/resources/config.json

After you run the above command, you can build and start the service:

cd /tmp/openapi-lambda

To test the service from another terminal:

curl http://localhost:8080/server/info
  • YAML Model

Given we have test openapi.yaml and config.json in light-rest-4j/src/test/resources folder, the following command line will generate a RESTful API at /tmp/openapi-yaml folder.

Working directory: light-codegen

cd ~/networknt/light-codegen
java -jar codegen-cli/target/codegen-cli.jar -f openapi -o /tmp/openapi-yaml -m light-rest-4j/src/test/resources/openapi.yaml -c light-rest-4j/src/test/resources/config.json

After you run the above command, you can build and start the service:

cd /tmp/openapi-yaml
mvn clean install exec:exec

To test the service from another terminal:

curl http://localhost:8080/server/info

The above example use local OpenAPI specification and config file. Let’s try to use files from github.com:

Working directory: light-codegen

rm -rf /tmp/openapi-petstore
cd ~/networknt/light-codegen
java -jar codegen-cli/target/codegen-cli.jar -f openapi -o /tmp/openapi-petstore -m https://raw.githubusercontent.com/networknt/model-config/master/rest/openapi/petstore/1.0.0/openapi.json -c https://raw.githubusercontent.com/networknt/model-config/master/rest/openapi/petstore/1.0.0/config.json

Please note that you need to use a raw url when accessing github files. The above command line will generate a petstore service in /tmp/openapi-petstore.

Given we have most of the model and config files in model-config repo, most generator input would from the rest folder in model-config. Here is the example to generate petstore. Assuming model-config is in the same workspace as light-codegen.

Working directory: networknt

rm -rf /tmp/openapi-petstore
cd ~/networknt
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o /tmp/openapi-petstore -m model-config/rest/openapi/petstore/1.0.0/openapi.json -c model-config/rest/openapi/petstore/1.0.0/config.json

  • Generate openapi specifications from source code (supports for code-first development)

To support code-first development, we provide the openapi specification generator to generate specifications from code. The command below demonstrates the usage of the specifcation generator. Please note that the configuration item specGeneration is required to generate specifications. For details of this configuration item, please see Config.

cd ~/networknt/light-codegen/code-cli/target
java -cp .:./codegen-cli.jar:/path/to/project/target/classes com.networknt.codegen.Cli -f openapi-spec -o /path/to/config/config.json

Config Parameterization

To support generating parameterized config file, we provide additional commend -p, --parameterize for codegen-cli

cd ~/networknt/light-codegen
java -jar codegen-cli/target/codegen-cli.jar -p examplePath/paramsCondig -f openapilambda -o /tmp/openapi-json -m light-rest-4j/src/test/resources/openapi.json -c light-rest-4j/src/test/resources/config.json

By executing the above command, a project containing custom parameterized configuration files will be generated. Each configuration file that exists on the specified path examplePath/paramsCondig will be parameterized and added to the target configuration file path.

For more details of config parameterization, please see Config.

Codegen Site

You can generate a Lambda project from the site https://codegen.lightapi.net with your OpenAPI specification and a config file.

  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“OpenAPI Lambda Generator” was last updated: July 5, 2021: fixes #275 checked and corrected grammar/spelling for majority of pages (#276) (b3bbb7b)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute