Class AbstractMapper

java.lang.Object
ru.yojo.codegen.mapper.AbstractMapper
Direct Known Subclasses:
MessageMapper, SchemaMapper

public class AbstractMapper extends Object
Base class for schema and message mapping logic.

Provides reusable methods to:

  • Populate VariableProperties from raw YAML maps
  • Handle various field types: primitives, collections, maps, enums, polymorphic schemas, and references
  • Apply validation annotations and required imports based on required and format constraints
  • Resolve inner/anonymous schemas and register them for separate generation

Subclasses SchemaMapper and MessageMapper extend this to implement schema- and message-specific logic.

  • Field Details

    • namingStrategy

      protected final NamingStrategy namingStrategy
      Naming strategy for field/method name transformations.
  • Constructor Details

    • AbstractMapper

      protected AbstractMapper(NamingStrategy namingStrategy, PropertyTypeResolver propertyTypeResolver)
      Creates an AbstractMapper with injected dependencies.
      Parameters:
      namingStrategy - the naming strategy to use
      propertyTypeResolver - the property type resolver (Chain of Responsibility)
    • AbstractMapper

      protected AbstractMapper(NamingStrategy namingStrategy)
      Creates an AbstractMapper with a specific naming strategy and a default resolver.
      Parameters:
      namingStrategy - the naming strategy to use
    • AbstractMapper

      protected AbstractMapper()
      Creates an AbstractMapper with default dependencies.
  • Method Details

    • collectPolymorphRefs

      public static List<Object> collectPolymorphRefs(Map<String,Object> propertiesMap)
      Collects all polymorphic references (oneOf, allOf, anyOf) from a properties map.

      This pattern is repeated in multiple places; extracting it here avoids duplication.

      Parameters:
      propertiesMap - the raw property definition map
      Returns:
      flat list of all polymorphic reference objects
    • fillProperties

      public void fillProperties(String schemaName, VariableProperties variableProperties, Map<String,Object> currentSchema, Map<String,Object> schemas, String propertyName, Map<String,Object> propertiesMap, ProcessContext processContext, Map<String,Object> innerSchemas)
      Parameters:
      schemaName - name of the containing schema (used for logging and inner schema naming)
      variableProperties - target field container to populate
      currentSchema - full schema map where this property is defined
      schemas - global map of all known schemas (for $ref resolution)
      propertyName - original YAML key (e.g., "email")
      propertiesMap - raw YAML map for this field (e.g., { type: string, format: email })
      processContext - current generation context (Spring Boot version, helper, etc.)
      innerSchemas - accumulator for discovered inner schemas (e.g., anonymous objects, enums)
    • fillVariableProperties

      public void fillVariableProperties(String schemaName, VariableProperties variableProperties, Map<String,Object> currentSchema, Map<String,Object> schemas, String propertyName, Map<String,Object> propertiesMap, ProcessContext processContext, Map<String,Object> innerSchemas)
      Resolves the concrete Java type and metadata for a field based on its raw YAML definition.

      Handles:

      • Maps (additionalProperties)
      • Arrays (type: array) with realization and nested types
      • References ($ref)
      • Plain/inner objects
      • Enums
      • format: existing for external classes
      • Polymorphic constructs (oneOf, allOf, anyOf)
      Parameters:
      schemaName - name of the containing schema
      variableProperties - field container to populate
      currentSchema - current schema map
      schemas - global schema registry
      propertyName - field name in YAML
      propertiesMap - raw field definition
      processContext - generation context
      innerSchemas - accumulator for inner schemas
    • prepareImport

      protected String prepareImport(ProcessContext ctx, String className)
      Prepares a full import string for a class in the common package.
      Parameters:
      ctx - process context to resolve common package
      className - simple class name
      Returns:
      full import, e.g., "com.example.common.MyClass;"
    • fillMapProperties

      protected void fillMapProperties(VariableProperties variableProperties, Map<String,Object> currentSchema, Map<String,Object> schemas, Map<String,Object> propertiesMap, ProcessContext processContext)
      Fills map-related properties (e.g., additionalProperties) into VariableProperties. Supports:
      • Primitive value types (e.g., Map<String, Integer>)
      • Custom object values (via $ref)
      • Nested collections (e.g., Map<String, List<User>>)
      • Custom key types (e.g., Map<UUID, ...>)
      • realization for map initialization
      Parameters:
      variableProperties - target field
      currentSchema - current schema map
      schemas - global schemas
      propertiesMap - field definition (must contain additionalProperties)
      processContext - context
    • fillExistingObjectProperties

      protected static void fillExistingObjectProperties(VariableProperties variableProperties)
      Configures VariableProperties for format: existing references. Sets type to the simple class name and adds the required import.
      Parameters:
      variableProperties - field to configure
    • fillEnumProperties

      protected void fillEnumProperties(String schemaName, VariableProperties variableProperties, String propertyName, Map<String,Object> propertiesMap, ProcessContext processContext, Map<String,Object> innerSchemas)
      Processes enum fields and registers the corresponding enum schema in innerSchemas.

      If x-enumNames is present, generates an enum with description field; otherwise, a plain enum.

      Parameters:
      schemaName - name of the parent schema
      variableProperties - field container
      propertyName - YAML key
      propertiesMap - raw enum definition
      processContext - context
      innerSchemas - accumulator
    • fillObjectProperties

      protected void fillObjectProperties(String schemaName, VariableProperties variableProperties, Map<String,Object> schemas, String propertyName, Map<String,Object> propertiesMap, ProcessContext processContext, Map<String,Object> innerSchemas)
      Processes inner/anonymous object schemas and registers them for separate DTO generation.

      Generates a unique name (e.g., ParentSchemaPropertyName) and stores the synthetic schema in innerSchemas.

      Parameters:
      schemaName - name of the parent schema
      variableProperties - field container
      schemas - full schema map for reference resolution
      propertyName - proposed inner schema name
      propertiesMap - raw object definition
      processContext - context
      innerSchemas - accumulator
    • fillReferenceProperties

      protected void fillReferenceProperties(String schemaName, VariableProperties variableProperties, Map<String,Object> currentSchema, Map<String,Object> schemas, String propertyName, Map<String,Object> propertiesMap, ProcessContext processContext, Map<String,Object> innerSchemas)
      Resolves $ref references and populates field type/imports accordingly.

      Handles recursive type resolution (e.g., $ref to scalar formats), detects referenced enums, and adds appropriate imports (local or external via package).

      Parameters:
      schemaName - current schema name
      variableProperties - field to configure
      currentSchema - current schema map
      schemas - global schema registry
      propertyName - YAML key
      propertiesMap - field definition (must contain $ref)
      processContext - generation context
      innerSchemas - accumulator
    • fillArrayProperties

      protected void fillArrayProperties(String schemaName, VariableProperties variableProperties, Map<String,Object> currentSchema, Map<String,Object> schemas, String propertyName, Map<String,Object> propertiesMap, ProcessContext processContext, Map<String,Object> innerSchemas)
      Processes array-type fields and configures collection type, element type, and realization.

      Special case: structurally empty items (no type, $ref, properties) defaults to List<Object>.

      Parameters:
      schemaName - current schema name
      variableProperties - field container
      currentSchema - current schema map
      schemas - global schemas
      propertyName - YAML key
      propertiesMap - field definition
      processContext - generation context
      innerSchemas - accumulator
    • fillCollectionWithExistingObject

      protected static void fillCollectionWithExistingObject(VariableProperties variableProperties, Map<String,Object> items)
      Configures collection realization and import for format: existing arrays.
      Parameters:
      variableProperties - field container
      items - items map from YAML
    • fillInnerSchema

      protected void fillInnerSchema(VariableProperties variableProperties, String propertyName, Map<String,Object> propertiesMap, ProcessContext processContext, Map<String,Object> innerSchemas)
      Registers an inner schema for subsequent DTO generation.

      Normalizes the schema (ensures type: object and extracts actual properties) before registration.

      Parameters:
      variableProperties - field container
      propertyName - proposed inner schema name
      propertiesMap - raw inner schema definition
      processContext - context
      innerSchemas - accumulator
    • fillMessageFromChannel

      public static void fillMessageFromChannel(Map<String,Object> allContent, Map<String,Object> messagesMap, Set<String> excludeSchemas, Map<String,Object> mapToMessage, String channelName, String channelType)
      Populates messagesMap from AsyncAPI 2.x channels/publish/subscribe sections.

      Resolves payload definitions, extracts polymorphic payloads (e.g., oneOf) and creates synthetic message definitions when needed.

      Parameters:
      allContent - full AsyncAPI document
      messagesMap - output map (schema name → payload)
      excludeSchemas - set of schema names to exclude from DTO generation
      mapToMessage - channel operation map (e.g., subscribe or publish)
      channelName - channel key
      channelType - "subscribe" or "publish"
    • fillCollectionType

      protected static void fillCollectionType(VariableProperties variableProperties)
      Sets the final Java collection type string (e.g., List<String>) based on collectionType.
      Parameters:
      variableProperties - field container (must have items set)
    • fillRequiredAnnotationsAndImports

      public static void fillRequiredAnnotationsAndImports(VariableProperties variableProperties, Map<String,Object> currentSchema, String propertyName)
      Applies validation annotations and imports based on required, validation groups, and field type.

      Generates:

      • @NotNull/@NotBlank/@NotEmpty for required fields
      • groups = {...} variants if validation groups are configured
      • @Valid for nested objects (excluding collections/enums)
      Parameters:
      variableProperties - field container (updated in-place)
      currentSchema - schema map (to read required, validationGroups, etc.)
      propertyName - field name (to check presence in required)
    • fillEnumSchema

      protected static void fillEnumSchema(String enumSchemaName, Map<String,Object> propertiesMap, Map<String,Object> innerSchemas)
      Registers an enum schema in innerSchemas based on raw YAML definition.

      Delegates to fillByEnumWithDescription(java.util.Map<java.lang.String, java.lang.Object>) or fillByEnum(java.util.List<java.lang.String>) depending on presence of x-enumNames.

      Parameters:
      enumSchemaName - enum schema name
      propertiesMap - raw enum definition
      innerSchemas - accumulator