Class AbstractJavaCodegen

    • Constructor Detail

      • AbstractJavaCodegen

        public AbstractJavaCodegen()
    • Method Detail

      • isConstructorWithAllArgsAllowed

        protected boolean isConstructorWithAllArgsAllowed​(CodegenModel codegenModel)
        trigger the generation of all arguments constructor or not. It avoids generating the same constructor twice.
        Returns:
        true if an allArgConstructor must be generated
      • applyJavaxPackage

        protected void applyJavaxPackage()
      • applyJakartaPackage

        protected void applyJakartaPackage()
      • applyJspecify

        protected void applyJspecify()
        Configure the generator for jspecify. override Nullable import to use the jspecify version. add package-info.java to the model and api packages.
      • toVarName

        public String toVarName​(String name)
        Description copied from class: DefaultCodegen
        Return the variable name by removing invalid characters and proper escaping if it's a reserved word.
        Overrides:
        toVarName in class DefaultCodegen
        Parameters:
        name - the variable name
        Returns:
        the sanitized variable name
      • toParamName

        public String toParamName​(String name)
        Description copied from class: DefaultCodegen
        Return the parameter name by removing invalid characters and proper escaping if it's a reserved word.
        Specified by:
        toParamName in interface CodegenConfig
        Overrides:
        toParamName in class DefaultCodegen
        Parameters:
        name - Codegen property object
        Returns:
        the sanitized parameter name
      • toModelName

        public String toModelName​(String name)
        Description copied from class: DefaultCodegen
        Converts the OpenAPI schema name to a model name suitable for the current code generator. May be overridden for each programming language. In case the name belongs to the TypeSystem it won't be renamed.
        Specified by:
        toModelName in interface CodegenConfig
        Overrides:
        toModelName in class DefaultCodegen
        Parameters:
        name - the name of the model
        Returns:
        capitalized model name
      • useBeanValidationOnMapValueType

        protected boolean useBeanValidationOnMapValueType()
        Whether bean validation of map values is expressed on the value type argument (Map<String, @Valid V>) instead of on the map itself. Generators that have migrated off the deprecated container-level @Valid (Hibernate Validator HV000271) override this to return true. Arrays/sets always place bean validation on the type argument, so they are not gated by this method.
        Returns:
        true to emit map-value bean validation on the type argument; false by default
      • getTypeDeclarationWithBeanValidation

        protected String getTypeDeclarationWithBeanValidation​(io.swagger.v3.oas.models.media.Schema<?> items)
        Renders the type declaration of a container element (array/set item or map value) with its bean validation applied to the type argument, e.g. @Valid Pet or @Size(max = 3) String. Hibernate Validator 9.1+ expects cascade/constraints on the type argument; a container-level @Valid is deprecated and logs HV000271, so the annotation is placed here instead of on the container.
        Parameters:
        items - the array/set item or map value schema
        Returns:
        the element type declaration prefixed with its bean validation (no prefix if none)
      • getExtraListAnnotation

        protected String getExtraListAnnotation​(io.swagger.v3.oas.models.media.Schema<?> items)
        get the annotations for list items.
        Parameters:
        items - the Schema of the array items.
        Returns:
        a String with the concatened annotations, or empty if none
      • getBeanValidation

        protected String getBeanValidation​(io.swagger.v3.oas.models.media.Schema<?> items)
        This method stand for resolve bean validation for a container element (array/set item or map value). Return empty if there's no bean validation for requested type or prop useBeanValidation false or missed.
        Parameters:
        items - type
        Returns:
        BeanValidation for declared element type of a container (array, set, map value)
      • toArrayDefaultValue

        public String toArrayDefaultValue​(CodegenProperty cp,
                                          io.swagger.v3.oas.models.media.Schema schema)
        Return the default value of array property

        Return null if there's no default value. Any non-null value will cause {{#defaultValue} check to pass.

        Parameters:
        cp - Codegen property
        schema - Property schema
        Returns:
        string presentation of the default value of the property
      • toDefaultValue

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

        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:
        cp - Codegen property
        schema - Property schema
        Returns:
        string presentation of the default value of the property
      • toDefaultParameterValue

        public String toDefaultParameterValue​(io.swagger.v3.oas.models.media.Schema<?> schema)
        Description copied from class: DefaultCodegen
        Return the default value of the parameter

        Return null if you do NOT want a default value. Any non-null value will cause {{#defaultValue} check to pass.

        Overrides:
        toDefaultParameterValue in class DefaultCodegen
        Parameters:
        schema - Parameter schema
        Returns:
        string presentation of the default value of the parameter
      • setParameterExampleValue

        public void setParameterExampleValue​(CodegenParameter codegenParameter,
                                             io.swagger.v3.oas.models.parameters.Parameter parameter)
        Return the example value of the parameter. Overrides the setParameterExampleValue(CodegenParameter, Parameter) method in DefaultCodegen to always call setParameterExampleValue(CodegenParameter) in this class, which adds single quotes around strings from the x-example property.
        Overrides:
        setParameterExampleValue in class DefaultCodegen
        Parameters:
        codegenParameter - Codegen parameter
        parameter - Parameter
      • setParameterExampleValue

        public void setParameterExampleValue​(CodegenParameter codegenParameter,
                                             io.swagger.v3.oas.models.parameters.RequestBody requestBody)
        Return the example value of the parameter. Overrides the parent method in DefaultCodegen to not set examples on complex models, as they don't compile properly.
        Overrides:
        setParameterExampleValue in class DefaultCodegen
        Parameters:
        codegenParameter - Codegen parameter
        requestBody - Request body
      • toExampleValue

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

        This method should be overridden in the generator to meet its requirement.

        Overrides:
        toExampleValue in class DefaultCodegen
        Parameters:
        p - Property schema
        Returns:
        string presentation of the example value of the property
      • getSchemaType

        public String getSchemaType​(io.swagger.v3.oas.models.media.Schema p)
        Description copied from class: DefaultCodegen
        returns the OpenAPI type for the property. Use getAlias to handle $ref of primitive type
        Overrides:
        getSchemaType in class DefaultCodegen
        Parameters:
        p - property schema
        Returns:
        string presentation of the type
      • toOperationId

        public String toOperationId​(String operationId)
        Description copied from class: DefaultCodegen
        Return the operation ID (method name)
        Overrides:
        toOperationId in class DefaultCodegen
        Parameters:
        operationId - operation ID
        Returns:
        the sanitized method name
      • fromModel

        public CodegenModel fromModel​(String name,
                                      io.swagger.v3.oas.models.media.Schema model)
        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
        model - OAS Model object
        Returns:
        Codegen Model object
      • removeAnnotations

        public String removeAnnotations​(String dataType)
        Remove annotations from the given data type string.

        For example:

        • @jakarta.annotation.Nullable String -> String
        • @Min(0) @Max(10)Integer -> Integer
        • @Pattern(regexp = "^[a-z]$")String> -> String
        • List<@Pattern(regexp = "^[a-z]$")String>" -> "List<String>"
        • List<@Valid Pet>" -> "List<Pet>"
        Parameters:
        dataType - the data type string
        Returns:
        the data type string without annotations
      • removeAnnotationsWithExclusion

        public String removeAnnotationsWithExclusion​(String dataType,
                                                     String annotationToKeep)
        Remove annotations from the given data type string except annotationToKeep.

        For example:

        • @Nullable @Min(0) @Max(10)Integer -> @Nullable Integer
        • @Nullable List<@Valid Pet>" -> "@Nullable List<Pet>"
        Parameters:
        dataType - the data type string
        annotationToKeep - annotation to keep. For example @Nullable
        Returns:
        the data type string without annotations.
      • sanitizeDataType

        public String sanitizeDataType​(String dataType)
        Sanitize the datatype. This will remove all characters except alphanumeric ones. It will also first use {removeAnnotations(String)} to remove the annotations added to the datatype
        Parameters:
        dataType - the data type string
        Returns:
        the data type string without annotations and any characters except alphanumeric ones
      • normalizeOperationParameterVendorExtensions

        protected void normalizeOperationParameterVendorExtensions​(CodegenOperation operation,
                                                                   String name)
        Normalizes an operation parameter vendor extension across all parameter collections. In this context, normalization means converting a missing value, a single string, or a list value into a predictable mutable List<String> on each parameter. The same parameter can appear in several operation collections, so the collections are de-duplicated before updating the extension map.
        Parameters:
        operation - operation whose parameters should be updated
        name - vendor extension name
      • needToImport

        protected boolean needToImport​(String type)
        Description copied from class: DefaultCodegen
        Check the type to see if it needs import the library/module/package
        Overrides:
        needToImport in class DefaultCodegen
        Parameters:
        type - name of the type
        Returns:
        true if the library/module/package of the corresponding type needs to be imported
      • toEnumVarName

        public String toEnumVarName​(String value,
                                    String datatype)
        Description copied from class: DefaultCodegen
        Return the sanitized variable name for enum
        Overrides:
        toEnumVarName in class DefaultCodegen
        Parameters:
        value - enum variable name
        datatype - data type
        Returns:
        the sanitized variable name for enum
      • toEnumValue

        public String toEnumValue​(String value,
                                  String datatype)
        Description copied from class: DefaultCodegen
        Return the enum value in the language specified format e.g. status becomes "status"
        Overrides:
        toEnumValue in class DefaultCodegen
        Parameters:
        value - enum variable name
        datatype - data type
        Returns:
        the sanitized value for enum
      • divideOperationsByContentType

        public List<io.swagger.v3.oas.models.Operation> divideOperationsByContentType​(io.swagger.v3.oas.models.OpenAPI openAPI,
                                                                                      String path,
                                                                                      String httpMethod,
                                                                                      io.swagger.v3.oas.models.Operation operation)
        A content-type variant is split off after preprocessOpenAPI(io.swagger.v3.oas.models.OpenAPI) stamped the operation it comes from, so it carries that operation's Content-Type and Accept, for every media-type it declares: the variants are stamped again here, each with the single media-type it was narrowed to on each axis.
        Specified by:
        divideOperationsByContentType in interface CodegenConfig
        Overrides:
        divideOperationsByContentType in class DefaultCodegen
        Parameters:
        openAPI - the OpenAPI document
        path - the resource path
        httpMethod - the HTTP method
        operation - the operation to (maybe) divide
        Returns:
        the operations to generate for operation (the operation itself when not divided)
      • fromOperation

        public CodegenOperation fromOperation​(String path,
                                              String httpMethod,
                                              io.swagger.v3.oas.models.Operation operation,
                                              List<io.swagger.v3.oas.models.servers.Server> servers)
        Description copied from class: DefaultCodegen
        Convert OAS Operation object to Codegen Operation object
        Specified by:
        fromOperation in interface CodegenConfig
        Overrides:
        fromOperation in class DefaultCodegen
        Parameters:
        path - the path of the operation
        httpMethod - HTTP method
        operation - OAS operation object
        servers - list of servers
        Returns:
        Codegen Operation object
      • getOutputTestFolder

        public String getOutputTestFolder()
      • toRegularExpression

        public String toRegularExpression​(String pattern)
        Description copied from class: DefaultCodegen
        Return the regular expression/JSON schema pattern (http://json-schema.org/latest/json-schema-validation.html#anchor33)
        Overrides:
        toRegularExpression in class DefaultCodegen
        Parameters:
        pattern - the pattern (regular expression)
        Returns:
        properly-escaped pattern
      • getterAndSetterCapitalize

        public String getterAndSetterCapitalize​(String name)
        Camelize the method name of the getter and setter
        Overrides:
        getterAndSetterCapitalize in class DefaultCodegen
        Parameters:
        name - string to be camelized
        Returns:
        Camelized string
      • postProcessFile

        public void postProcessFile​(File file,
                                    String fileType)
        Description copied from class: DefaultCodegen
        Post-process the auto-generated file, e.g. using go-fmt to format the Go code. The file type can be "model-test", "model-doc", "model", "api", "api-test", "api-doc", "supporting-file", "openapi-generator-ignore", "openapi-generator-version"

        TODO: store these values in enum instead

        Specified by:
        postProcessFile in interface CodegenConfig
        Overrides:
        postProcessFile in class DefaultCodegen
        Parameters:
        file - file to be processed
        fileType - file type
      • addAdditionPropertiesToCodeGenModel

        protected void addAdditionPropertiesToCodeGenModel​(CodegenModel codegenModel,
                                                           io.swagger.v3.oas.models.media.Schema schema)
        Description copied from class: DefaultCodegen
        Handle the model for the 'additionalProperties' keyword in the OAS schema.
        Overrides:
        addAdditionPropertiesToCodeGenModel in class DefaultCodegen
        Parameters:
        codegenModel - The codegen representation of the schema.
        schema - The input OAS schema.
      • handleImplicitHeaders

        protected void handleImplicitHeaders​(CodegenOperation operation)
        This method removes all implicit header parameters from the list of parameters
        Parameters:
        operation - - operation to be processed
      • setEnumPropertyNaming

        public void setEnumPropertyNaming​(String enumPropertyNamingType)
        Sets the naming convention for Java enum properties
        Parameters:
        enumPropertyNamingType - The string representation of the naming convention, as defined by AbstractJavaCodegen.ENUM_PROPERTY_NAMING_TYPE
      • addMustacheLambdas

        protected com.google.common.collect.ImmutableMap.Builder<String,​com.samskivert.mustache.Mustache.Lambda> addMustacheLambdas()
        Description copied from class: DefaultCodegen
        Preset map builder with commonly used Mustache lambdas. To extend the map, override addMustacheLambdas(), call parent method first and then add additional lambdas to the returned builder. If common lambdas are not desired, override addMustacheLambdas() method and return empty builder. Corresponding user documentation: docs/templating.md, section "Mustache Lambdas"
        Overrides:
        addMustacheLambdas in class DefaultCodegen
        Returns:
        preinitialized map with common lambdas
      • addNullableImportForOperation

        protected void addNullableImportForOperation​(CodegenOperation codegenOperation)
        Adds Nullable import if any parameter is nullable or optional.