Class CppHttplibServerCodegen

  • All Implemented Interfaces:
    CodegenConfig

    public class CppHttplibServerCodegen
    extends AbstractCppCodegen
    C++ HTTP Library Server Code Generator. This code generator creates C++ server stubs using the httplib library for handling HTTP requests. It generates: - Model classes with proper C++ typing (std::string, std::vector, std::optional, etc.) - API handler interfaces with type-safe request/response handling - CMake build configuration - JSON serialization/deserialization using nlohmann::json Key features: - Supports nullable types using std::optional - Proper namespace organization for models and APIs - Status code to error type mapping for responses - Standard C++ container types (vector, map, etc.) - Automatic include generation for dependencies
    Author:
    OpenAPI Generator Contributors

    Mustache templates are located in src/main/resources/cpp-httplib-server/.

    • Constructor Detail

      • CppHttplibServerCodegen

        public CppHttplibServerCodegen()
        Constructor for CppHttplibServerCodegen. Initializes the code generator with C++ httplib server specific configurations, type mappings, and template files.
    • Method Detail

      • getHelp

        public String getHelp()
        Returns a brief description of what this code generator does.
        Specified by:
        getHelp in interface CodegenConfig
        Overrides:
        getHelp in class DefaultCodegen
        Returns:
        help text describing the generator's purpose
      • toModelImport

        public String toModelImport​(String name)
        Converts a model name to its corresponding import statement. Handles both standard C++ types and custom model types.
        Specified by:
        toModelImport in interface CodegenConfig
        Overrides:
        toModelImport in class DefaultCodegen
        Parameters:
        name - the model name to import
        Returns:
        the appropriate #include directive or empty string
      • postProcessOperationsWithModels

        public OperationsMap postProcessOperationsWithModels​(OperationsMap objs,
                                                             List<ModelMap> allModels)
        Post-processes operations after model processing to add C++ specific configurations. This method: - Sets up class names and namespaces for API classes - Processes request and response models for each operation - Maps response status codes to their corresponding types - Collects all used models for proper include generation
        Specified by:
        postProcessOperationsWithModels in interface CodegenConfig
        Overrides:
        postProcessOperationsWithModels in class DefaultCodegen
        Parameters:
        objs - the operations map to process
        allModels - list of all models in the API
        Returns:
        the processed operations map
      • postProcessAllModels

        public Map<String,​ModelsMap> postProcessAllModels​(Map<String,​ModelsMap> objs)
        Post-processes all models to add C++ specific configurations and namespace handling. This method: - Computes model namespaces based on class names - Filters imports to exclude primitives and standard types - Processes model variables for proper C++ typing - Sets up proper getter/setter configurations
        Specified by:
        postProcessAllModels in interface CodegenConfig
        Overrides:
        postProcessAllModels in class AbstractCppCodegen
        Parameters:
        objs - map of all models to process
        Returns:
        the processed models map
      • toModelName

        public String toModelName​(String name)
        Converts an OpenAPI model name to a C++ class name. Handles inline model prefixes and converts to PascalCase.
        Specified by:
        toModelName in interface CodegenConfig
        Overrides:
        toModelName in class AbstractCppCodegen
        Parameters:
        name - the original model name
        Returns:
        the C++ class name
      • toApiName

        public String toApiName​(String name)
        Converts an API name to a C++ API class name. Handles default naming and path-based names.
        Specified by:
        toApiName in interface CodegenConfig
        Overrides:
        toApiName in class AbstractCppCodegen
        Parameters:
        name - the original API name
        Returns:
        the C++ API class name
      • fromModel

        public CodegenModel fromModel​(String name,
                                      io.swagger.v3.oas.models.media.Schema schema)
        Description copied from class: DefaultCodegen
        Convert OAS Model object to Codegen Model object.
        Specified by:
        fromModel in interface CodegenConfig
        Overrides:
        fromModel in class DefaultCodegen
        Parameters:
        name - the name of the model
        schema - OAS Model object
        Returns:
        Codegen Model object
      • toDefaultValue

        public String toDefaultValue​(io.swagger.v3.oas.models.media.Schema p)
        Description copied from class: DefaultCodegen
        Return the default value of the property

        This method should be overridden in the generator to meet its requirement. Return null if you do NOT want a default value. Any non-null value will cause {{#defaultValue} check to pass.

        Overrides:
        toDefaultValue in class DefaultCodegen
        Parameters:
        p - Property schema
        Returns:
        string presentation of the default value of the property
      • addProjectOrDefaultName

        public String addProjectOrDefaultName​(String name,
                                              String defaultName)
        Adds project name to a class name or returns default name if project name is not set.
        Parameters:
        name - the base name
        defaultName - the default name to use if input is null/empty
        Returns:
        the project-prefixed name or default name
      • toModelNamespace

        public String toModelNamespace​(String modelNamespace)
        Converts a model namespace string to proper format.
        Parameters:
        modelNamespace - the namespace to convert
        Returns:
        the formatted model namespace
      • toPascalCase

        public String toPascalCase​(String name)
        Converts a string to PascalCase formatting with consistent handling. Handles hyphens, underscores, and camelCase boundaries. Used for class names, type names, and enum names.
        Parameters:
        name - the string to convert
        Returns:
        the PascalCase formatted string
      • toCamelCase

        public String toCamelCase​(String name)
        Converts a string to camelCase formatting. Used for variable names and method names.
        Parameters:
        name - the string to convert
        Returns:
        the camelCase formatted string
      • camelize

        public String camelize​(String name,
                               boolean lowercaseFirstLetter)
        Converts a string to camelCase or PascalCase with proper case handling. Handles uppercase words like "POST" -> "Post" and "GET" -> "Get".
        Parameters:
        name - the string to convert
        lowercaseFirstLetter - whether to lowercase the first letter (camelCase)
        Returns:
        the camelized string
      • toHandlerFunctionName

        public String toHandlerFunctionName​(String httpMethod,
                                            String path,
                                            Boolean prefix)
        Generates a handler function name from HTTP method and path.
        Parameters:
        httpMethod - the HTTP method (GET, POST, etc.)
        path - the API path
        Returns:
        the generated handler function name
      • toHandlerFunctionRequest

        public String toHandlerFunctionRequest​(String path,
                                               String method)
        Generates a handler function request type from the API Path
        Parameters:
        path - the API path
        Returns:
        the generated handler function request
      • toHandlerFunctionResponse

        public String toHandlerFunctionResponse​(String path,
                                                String method)
        Generates a handler function response type from the API path.
        Parameters:
        path - the API path
        Returns:
        the generated handler function response