OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec
—
Helper classes and utilities for schema analysis, string manipulation, version handling, and model processing used throughout the OpenAPI Generator framework.
Static utility methods for working with OpenAPI models and schemas.
class ModelUtils {
// Schema type checking
public static boolean isObjectSchema(Schema schema);
public static boolean isComposedSchema(Schema schema); // oneOf/anyOf/allOf schemas
public static boolean isMapSchema(Schema schema);
public static boolean isArraySchema(Schema schema);
public static boolean isStringSchema(Schema schema);
public static boolean isIntegerSchema(Schema schema);
public static boolean isNumberSchema(Schema schema);
public static boolean isBooleanSchema(Schema schema);
public static boolean isNullType(Schema schema);
public static boolean isAnyType(Schema schema);
// Format-specific schema checking
public static boolean isDateSchema(Schema schema); // date format strings
public static boolean isDateTimeSchema(Schema schema); // date-time format strings
public static boolean isByteArraySchema(Schema schema); // byte format strings
public static boolean isBinarySchema(Schema schema); // binary format strings
public static boolean isFileSchema(Schema schema); // file upload schemas
public static boolean isUUIDSchema(Schema schema); // uuid format strings
public static boolean isEmailSchema(Schema schema); // email format strings
public static boolean isPasswordSchema(Schema schema); // password format strings
// Schema analysis and discovery
public static List<String> getAllUsedSchemas(OpenAPI openAPI); // Find all referenced schemas
public static List<String> getUnusedSchemas(OpenAPI openAPI); // Find unreferenced schemas
public static String getSimpleRef(String ref); // Extract name from $ref URL
public static Schema unaliasSchema(Schema schema, OpenAPI openAPI); // Resolve schema aliases/references
// Model processing utilities
public static CodegenModel getModelByName(final String name, final Map<String, ModelsMap> models);
public static boolean isGenerateAliasAsModel(); // Check alias-as-model setting
public static void setGenerateAliasAsModel(boolean value); // Configure alias-as-model behavior
}String manipulation utilities for code generation naming conventions.
class StringUtils {
// Case transformation
public static String camelize(String word); // Convert to camelCase
public static String camelize(String word, CamelizeOption camelizeOption); // With options
public static String underscore(String word); // Convert to snake_case
public static String dashize(String word); // Convert to dash-case
// Text escaping and sanitization
public static String escape(String input, Map<String, String> mapping,
Set<String> charactersToAllow, Set<String> charactersToEscape);
}
enum CamelizeOption {
LOWERCASE_FIRST_LETTER, // Start with lowercase letter
UPPERCASE_FIRST_LETTER // Start with uppercase letter (PascalCase)
}Semantic version handling and comparison utilities.
class SemVer implements Comparable<SemVer> {
// Constructors
public SemVer(String version); // Parse version string (e.g., "1.2.3-alpha+build")
// Version component access
public int getMajor(); // Get major version number
public int getMinor(); // Get minor version number
public int getPatch(); // Get patch version number
public String getPrerelease(); // Get prerelease identifier (alpha, beta, etc.)
public String getBuild(); // Get build metadata
// Comparison methods
public int compareTo(SemVer other); // Standard comparison (-1, 0, 1)
public boolean isGreaterThan(SemVer other); // Check if this > other
public boolean isLessThan(SemVer other); // Check if this < other
public boolean isEquivalentTo(SemVer other); // Check if versions are equivalent
// String representation
public String getVersion(); // Get normalized version string
public String toString(); // String representation
}public class SchemaProcessor {
public void processOpenAPIDocument(OpenAPI openAPI) {
// Find all schemas referenced in the document
List<String> usedSchemas = ModelUtils.getAllUsedSchemas(openAPI);
System.out.println("Referenced schemas: " + usedSchemas.size());
// Identify unused schemas for cleanup
List<String> unusedSchemas = ModelUtils.getUnusedSchemas(openAPI);
if (!unusedSchemas.isEmpty()) {
System.out.println("Unused schemas that could be removed: " + unusedSchemas);
}
// Process each schema in components
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
for (Map.Entry<String, Schema> entry : schemas.entrySet()) {
String schemaName = entry.getKey();
Schema schema = entry.getValue();
processSchema(schemaName, schema, openAPI);
}
}
private void processSchema(String name, Schema schema, OpenAPI openAPI) {
// Resolve any aliases or references
Schema resolvedSchema = ModelUtils.unaliasSchema(schema, openAPI);
if (ModelUtils.isObjectSchema(resolvedSchema)) {
processObjectSchema(name, resolvedSchema);
} else if (ModelUtils.isArraySchema(resolvedSchema)) {
processArraySchema(name, resolvedSchema);
} else if (ModelUtils.isStringSchema(resolvedSchema)) {
processStringSchema(name, resolvedSchema);
} else if (ModelUtils.isNumberSchema(resolvedSchema) || ModelUtils.isIntegerSchema(resolvedSchema)) {
processNumericSchema(name, resolvedSchema);
} else if (ModelUtils.isComposedSchema(resolvedSchema)) {
processComposedSchema(name, resolvedSchema);
}
}
private void processStringSchema(String name, Schema schema) {
if (ModelUtils.isDateSchema(schema)) {
System.out.println(name + " is a date field");
} else if (ModelUtils.isDateTimeSchema(schema)) {
System.out.println(name + " is a date-time field");
} else if (ModelUtils.isEmailSchema(schema)) {
System.out.println(name + " is an email field");
} else if (ModelUtils.isUUIDSchema(schema)) {
System.out.println(name + " is a UUID field");
} else if (ModelUtils.isBinarySchema(schema)) {
System.out.println(name + " is binary data");
}
}
}public class NamingProcessor {
public void demonstrateStringTransformations() {
String input = "user_account_details";
// Convert to different naming conventions
String camelCase = StringUtils.camelize(input);
// Result: "userAccountDetails"
String pascalCase = StringUtils.camelize(input, CamelizeOption.UPPERCASE_FIRST_LETTER);
// Result: "UserAccountDetails"
String dashCase = StringUtils.dashize(camelCase);
// Result: "user-account-details"
String snakeCase = StringUtils.underscore(pascalCase);
// Result: "user_account_details"
System.out.println("Original: " + input);
System.out.println("camelCase: " + camelCase);
System.out.println("PascalCase: " + pascalCase);
System.out.println("dash-case: " + dashCase);
System.out.println("snake_case: " + snakeCase);
}
public String generateMethodName(String operationId) {
// Clean and transform operation ID to method name
String cleaned = operationId.replaceAll("[^a-zA-Z0-9_]", "_");
return StringUtils.camelize(cleaned, CamelizeOption.LOWERCASE_FIRST_LETTER);
}
public String generateClassName(String schemaName) {
// Transform schema name to class name
String cleaned = schemaName.replaceAll("[^a-zA-Z0-9_]", "_");
return StringUtils.camelize(cleaned, CamelizeOption.UPPERCASE_FIRST_LETTER);
}
public String sanitizeText(String input) {
// Create custom escaping rules
Map<String, String> escapeMapping = new HashMap<>();
escapeMapping.put("\"", "\\\"");
escapeMapping.put("\n", "\\n");
escapeMapping.put("\t", "\\t");
Set<String> allowedChars = Set.of("a-z", "A-Z", "0-9", " ", "_", "-");
Set<String> escapeChars = Set.of("\"", "\n", "\t", "\\");
return StringUtils.escape(input, escapeMapping, allowedChars, escapeChars);
}
}public class VersionManager {
public void compareVersions() {
SemVer current = new SemVer("2.1.0");
SemVer minimum = new SemVer("2.0.0");
SemVer target = new SemVer("2.2.0-beta");
SemVer latest = new SemVer("3.0.0");
// Basic comparisons
if (current.isGreaterThan(minimum)) {
System.out.println("Current version meets minimum requirement");
}
if (current.isLessThan(target)) {
System.out.println("Upgrade available to " + target.getVersion());
}
// Version component access
System.out.println("Current version components:");
System.out.println(" Major: " + current.getMajor());
System.out.println(" Minor: " + current.getMinor());
System.out.println(" Patch: " + current.getPatch());
if (target.getPrerelease() != null) {
System.out.println("Target is prerelease: " + target.getPrerelease());
}
// Sorting versions
List<SemVer> versions = Arrays.asList(latest, current, minimum, target);
Collections.sort(versions);
System.out.println("Versions in ascending order:");
for (SemVer version : versions) {
System.out.println(" " + version.toString());
}
}
public boolean isCompatibleVersion(String requiredVersion, String actualVersion) {
SemVer required = new SemVer(requiredVersion);
SemVer actual = new SemVer(actualVersion);
// Check if major versions match and actual is >= required
return actual.getMajor() == required.getMajor() &&
actual.isGreaterThan(required) || actual.isEquivalentTo(required);
}
public SemVer findLatestCompatibleVersion(List<String> availableVersions, String minimumVersion) {
SemVer minimum = new SemVer(minimumVersion);
return availableVersions.stream()
.map(SemVer::new)
.filter(version -> version.getMajor() == minimum.getMajor()) // Same major version
.filter(version -> version.isGreaterThan(minimum) || version.isEquivalentTo(minimum))
.max(SemVer::compareTo)
.orElse(null);
}
}public class ModelProcessor {
public void processGeneratedModels(Map<String, ModelsMap> allModels) {
for (Map.Entry<String, ModelsMap> entry : allModels.entrySet()) {
String packageName = entry.getKey();
ModelsMap modelsMap = entry.getValue();
System.out.println("Processing package: " + packageName);
for (ModelMap modelMap : modelsMap.getModels()) {
CodegenModel model = (CodegenModel) modelMap.get("model");
processModel(model);
}
}
}
private void processModel(CodegenModel model) {
System.out.println("Processing model: " + model.classname);
// Find model by name in all models (utility method)
// CodegenModel foundModel = ModelUtils.getModelByName(model.name, allModels);
// Process model properties
for (CodegenProperty property : model.vars) {
processProperty(property, model);
}
// Handle inheritance
if (model.parent != null) {
System.out.println(" Extends: " + model.parent);
}
if (model.interfaces != null && !model.interfaces.isEmpty()) {
System.out.println(" Implements: " + String.join(", ", model.interfaces));
}
}
private void processProperty(CodegenProperty property, CodegenModel parentModel) {
// Apply naming conventions
String getterName = "get" + StringUtils.camelize(property.name, CamelizeOption.UPPERCASE_FIRST_LETTER);
String setterName = "set" + StringUtils.camelize(property.name, CamelizeOption.UPPERCASE_FIRST_LETTER);
property.getter = getterName;
property.setter = setterName;
// Add custom vendor extensions
property.vendorExtensions.put("x-custom-getter", getterName);
property.vendorExtensions.put("x-custom-setter", setterName);
property.vendorExtensions.put("x-parent-model", parentModel.classname);
System.out.println(" Property: " + property.name + " (" + property.dataType + ")");
}
}public class ReferenceResolver {
public void resolveSchemaReferences(OpenAPI openAPI) {
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
for (Map.Entry<String, Schema> entry : schemas.entrySet()) {
String schemaName = entry.getKey();
Schema schema = entry.getValue();
resolveSchemaRefs(schemaName, schema, openAPI);
}
}
private void resolveSchemaRefs(String name, Schema schema, OpenAPI openAPI) {
if (schema.get$ref() != null) {
// Extract reference name
String refName = ModelUtils.getSimpleRef(schema.get$ref());
System.out.println("Schema " + name + " references: " + refName);
// Resolve the reference
Schema resolvedSchema = ModelUtils.unaliasSchema(schema, openAPI);
if (resolvedSchema != schema) {
System.out.println(" Resolved to actual schema");
processResolvedSchema(name, resolvedSchema, openAPI);
}
}
// Process properties that might contain references
if (schema.getProperties() != null) {
for (Map.Entry<String, Schema> propEntry : schema.getProperties().entrySet()) {
String propName = propEntry.getKey();
Schema propSchema = propEntry.getValue();
if (propSchema.get$ref() != null) {
String propRefName = ModelUtils.getSimpleRef(propSchema.get$ref());
System.out.println(" Property " + propName + " references: " + propRefName);
}
}
}
}
private void processResolvedSchema(String name, Schema schema, OpenAPI openAPI) {
// Process the resolved schema
if (ModelUtils.isObjectSchema(schema)) {
System.out.println(" Resolved to object schema");
} else if (ModelUtils.isArraySchema(schema)) {
System.out.println(" Resolved to array schema");
}
// ... other processing
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-openapitools--openapi-generator