OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec
npx @tessl/cli install tessl/maven-org-openapitools--openapi-generator@7.14.0OpenAPI Generator is a comprehensive code generation framework that automatically creates API client libraries, server stubs, documentation, and configuration files from OpenAPI Specification documents (versions 2.0 and 3.0). It provides extensive customization through templates and configuration, supporting over 50 programming languages and frameworks.
<dependency><groupId>org.openapitools</groupId><artifactId>openapi-generator</artifactId><version>7.14.0</version></dependency>import org.openapitools.codegen.*;
import org.openapitools.codegen.config.CodegenConfigurator;import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.config.CodegenConfigurator;
// Configure the generator
CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setInputSpec("https://petstore.swagger.io/v2/swagger.json")
.setOutputDir("./generated-code");
// Generate the code
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();OpenAPI Generator is built around several key components:
Primary interfaces and classes for setting up and executing code generation workflows. Essential for creating custom generators and configuring generation processes.
interface CodegenConfig {
String getName();
CodegenType getTag();
GeneratorLanguage generatorLanguage();
// Model and operation conversion
CodegenModel fromModel(String name, Schema schema);
CodegenOperation fromOperation(String resourcePath, String httpMethod,
Operation operation, List<Server> servers);
// Post-processing
Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs);
OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels);
}
interface Generator {
Generator opts(ClientOptInput opts);
List<File> generate();
}Configuration classes and builders for setting up generation parameters, including input specifications, output directories, and generator options.
class CodegenConfigurator {
public static CodegenConfigurator fromFile(String configFile, Module... modules);
public CodegenConfigurator setGeneratorName(String generatorName);
public CodegenConfigurator setInputSpec(String inputSpec);
public CodegenConfigurator setOutputDir(String outputDir);
public CodegenConfigurator addAdditionalProperty(String key, Object value);
public ClientOptInput toClientOptInput();
}
class ClientOptInput {
public ClientOptInput openAPI(OpenAPI openAPI);
public ClientOptInput config(CodegenConfig codegenConfig);
}Classes representing OpenAPI schemas, operations, parameters, and responses as Java objects for template processing and code generation.
class CodegenModel implements IJsonSchemaValidationProperties {
public String name; // Schema name
public String classname; // Generated class name
public String description; // Model description
public List<CodegenProperty> vars; // Model properties
public List<CodegenProperty> requiredVars; // Required properties
public String parent; // Parent class name
public List<String> interfaces; // Implemented interfaces
}
class CodegenOperation {
public String path; // URL path
public String operationId; // Operation identifier
public String httpMethod; // HTTP method
public String nickname; // Generated method name
public List<CodegenParameter> allParams; // All parameters
public List<CodegenResponse> responses; // Response definitions
public String returnType; // Return type
}JSON Schema validation properties and type checking utilities for ensuring proper data handling and constraint validation.
interface IJsonSchemaValidationProperties {
// Type identification
boolean getIsModel();
boolean getIsArray();
boolean getIsString();
boolean getIsNumber();
// Validation constraints
String getPattern();
String getMinimum();
String getMaximum();
Integer getMinLength();
Integer getMaxLength();
// Schema relationships
CodegenProperty getItems();
List<CodegenProperty> getVars();
}Helper classes and utilities for schema analysis, string manipulation, version handling, and model processing.
class ModelUtils {
public static boolean isObjectSchema(Schema schema);
public static boolean isArraySchema(Schema schema);
public static boolean isStringSchema(Schema schema);
public static List<String> getAllUsedSchemas(OpenAPI openAPI);
public static Schema unaliasSchema(Schema schema, OpenAPI openAPI);
}
class StringUtils {
public static String camelize(String word);
public static String underscore(String word);
public static String dashize(String word);
}enum CodegenType {
CLIENT, // Client library generation
SERVER, // Server stub generation
DOCUMENTATION, // Documentation generation
SCHEMA, // Schema-only generation
CONFIG, // Configuration file generation
OTHER // Other/custom generation types
}
enum GeneratorLanguage {
JAVA, PYTHON, JAVASCRIPT, TYPESCRIPT, C_SHARP, GO, RUBY, PHP,
SWIFT, KOTLIN, SCALA, C_PLUS_PLUS, RUST, // ... 50+ languages
}
class SpecValidationException extends RuntimeException {
public Set<String> getErrors();
public Set<String> getWarnings();
}