The Generator module is a core component of the XAP Java SDK. It automatically produces SDK code from OpenAPI specifications. It leverages the Expedia Group SDK Generator to transform API specifications into type-safe, consistent, and well-documented client code.
The generator is customized to produce models and operations from the provided specification; requests execution logic is handled by other manually-implemented classes that delegate the request execution to an internal Expedia Group SDK core modules.
- EG SDK Generator Plugin Configuration
- Key Tasks
- Generation Workflow
- Custom Templates
- Output Directory
- License
The generator plugin configurations are defined in generator/build.gradle.kts in the egSdkGenerator extension.
egSdkGenerator {
namespace = "xap"
basePackage = "com.expediagroup.sdk.xap"
specFilePath = File("$rootDir/specs/transformed-spec.yaml")
objectMapper = "com.expediagroup.sdk.xap.configuration.OBJECT_MAPPER"
outputDir = File("$rootDir/xap-sdk/src/main/kotlin")
customTemplatesDir = File("$projectDir/src/main/resources/templates")
}The module provides several Gradle tasks to facilitate the code generation process:
| Task | Description |
|---|---|
mergeSpecs |
Merges multiple OpenAPI spec files into a single specs.yaml file |
transformSpecs |
Transforms the merged spec file with Expedia Group-specific customizations |
generateEgSdk |
Generates the SDK code from the transformed spec file |
generateAndFormat |
Generates SDK code and formats it with ktlint |
XAP Java SDK is generated from a collection of OpenAPI specifications that are merged and transformed into a single spec file. The final transformed spec file is then fed to the SDK generator.
Caution
Some tasks invoke npx under the hood, so ensure npm is installed.
A typical workflow to generate new models and operations involves the following steps:
Note
If the spec is a new, it should be added to the specs/openapi-merge.json file.
./gradlew :generator:mergeSpecsThis task merges all OpenAPI spec files into a single specs.yaml file.
./gradlew :generator:transformSpecsThis task transforms the merged spec file into a single file transformed-spec.yaml suitable for the SDK generator.
./gradlew :generator:generateAndFormat
# Or without formatting
./gradlew :generator:generateEgSdkThis task generates the SDK code based on the transformed spec file and places it in the specified output directory.
flowchart TD
%% Nodes
start([Start])
decision{{ }}
newSpec[Place the new spec in the **specs** folder]
addEntry[Add file entry to **specs/openapi-merge.json**]
updateSpec[Apply the changes to the targeted spec file]
mergeTask[Run **Specs Merge** task]
transformTask[Run **Transform Spec** task]
generator[Run **SDK Generator** <br/>& verify changes]
%% Edges
start --> decision
decision -->|New Spec?| newSpec
decision -->|Existing Spec?| updateSpec
newSpec --> addEntry
addEntry --> mergeTask
updateSpec --> mergeTask
mergeTask --> transformTask
transformTask --> generator
Expedia Group XAP SDK generator allows users to define custom Mustache templates for code generation. You can supply Mustache templates to customise or extend the generated code. Custom templates either override a default file or add new artifacts.
To override the default templates, the custom templates should match the name and location of the default templates in the generator plugin. You can find the generator's default templates here
As a living example, XAP SDK overrides the operations/params/builder.mustache template.
The generator plugin will pick up the custom templates automatically without any additional configuration needed.
The OpenAPI generator allows adding new templates for specific use cases. There are two types of templates that can be added:
Templates that the generator invokes for each API operation.
egSdkGenerator {
apiTemplates = listOf(
ApiTemplate(
// Template file name - relative to the customTemplatesDir
templateFile = "my-custom-api-template.mustache",
// output destination - relative to the outputDir
destinationPath = "com/expediagroup/sdk/xap/operation/custom",
// Optional suffix appended to the generated file name. The base name is the operation name.
fileNameSuffix = "CustomOperation.kt",
),
)
}Templates that the generator invokes once to generate the supporting code for the SDK.
egSdkGenerator {
supportingTemplates = listOf(
SupportingTemplate(
// Template file name - relative to the customTemplatesDir
templateFile = "room.mustache",
// output destination - relative to the outputDir
destinationPath = "com/expediagroup/sdk/xap/model",
// The name of the generated file
fileName = "Room.kt",
),
)
}The generator produces code that is integrated into the main xap-sdk module (src/main/kotlin).
This module is part of the XAP Java SDK and distributed under the Apache 2.0 license.