CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-springfox--springfox-swagger2

Swagger 2 API documentation integration library for Spring WebMVC and WebFlux applications

Pending
Overview
Eval results
Files

json-serialization.mddocs/

JSON Serialization

Customized Jackson module for proper Swagger 2 JSON serialization with support for vendor extensions, example values, and Swagger-specific formatting requirements.

Capabilities

Swagger2JacksonModule

Custom Jackson module that configures JSON serialization for Swagger 2 objects, ensuring proper formatting and compliance with Swagger 2 specification requirements.

/**
 * Jackson module for customizing Swagger 2 JSON serialization
 * Extends SimpleModule and implements JacksonModuleRegistrar for Spring integration
 */
public class Swagger2JacksonModule extends SimpleModule implements JacksonModuleRegistrar {
    
    /**
     * Conditionally registers this module with the provided ObjectMapper
     * Only registers if no existing Swagger mix-in is already configured
     * Also disables FAIL_ON_EMPTY_BEANS serialization failure
     * 
     * @param objectMapper The Jackson ObjectMapper to register with
     */
    public void maybeRegisterModule(ObjectMapper objectMapper);
    
    /**
     * Sets up Jackson serialization mix-ins for Swagger 2 types
     * Configures custom serialization behavior for all Swagger model objects
     * 
     * @param context Jackson module setup context
     */
    public void setupModule(SetupContext context);
}

Usage Example:

// Typically configured automatically by Spring Boot
ObjectMapper objectMapper = new ObjectMapper();
Swagger2JacksonModule module = new Swagger2JacksonModule();
module.maybeRegisterModule(objectMapper);

// Manual registration
objectMapper.registerModule(new Swagger2JacksonModule());

Serialization Mix-ins

The module configures custom serialization behavior for all major Swagger 2 types:

/**
 * Mix-in class that configures common serialization settings for Swagger types
 * Applied to: Swagger, Info, License, Scheme, SecurityRequirement, SecuritySchemeDefinition,
 * Model, Operation, Path, Parameter, ExternalDocs, Xml, Tag, Contact
 */
@JsonAutoDetect
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
private class CustomizedSwaggerSerializer {
}

/**
 * Mix-in class specifically for Response objects
 * Ignores the 'responseSchema' property during serialization
 */
@JsonAutoDetect
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
@JsonIgnoreProperties("responseSchema")
private class ResponseSerializer {
}

Property Example Serialization

Custom serializer for handling example values in API documentation with proper JSON formatting.

/**
 * Mix-in interface for Property example serialization
 * Applies custom serialization logic to example values
 */
@JsonAutoDetect
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
private interface PropertyExampleSerializerMixin {
    
    /**
     * Custom serialization for example values
     * Handles string literals, JSON objects, arrays, and primitive values
     */
    @JsonSerialize(using = PropertyExampleSerializer.class)
    Object getExample();
}

/**
 * Custom Jackson serializer for property example values
 * Handles various data types and JSON formatting requirements
 */
public static class PropertyExampleSerializer extends StdSerializer<Object> {
    
    /**
     * Serializes example values with proper JSON formatting
     * Handles string literals, JSON objects, arrays, and primitive types
     * 
     * @param value The example value to serialize
     * @param gen Jackson JSON generator
     * @param provider Serializer provider
     */
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException;
    
    /**
     * Determines if a value should be considered empty
     * @param value The value to check
     * @return true if the value is null or empty string
     */
    public boolean isEmpty(Object value);
}

Serialization Features

Non-Empty Value Inclusion

All Swagger objects are configured with JsonInclude.Include.NON_EMPTY, which means:

  • Null values are not included in JSON output
  • Empty strings are not included
  • Empty collections are not included
  • This produces cleaner, more compact Swagger JSON

Auto-Detection Configuration

The module uses @JsonAutoDetect to automatically detect:

  • Public fields
  • Public getter methods
  • Public setter methods (for deserialization)

Example Value Handling

The custom example serializer handles various data types:

// String literals (quoted strings)
"example": "hello world"

// JSON objects 
"example": {"key": "value", "number": 42}

// JSON arrays
"example": [1, 2, 3, "item"]

// Boolean values
"example": true

// Numeric values  
"example": 42.5

// Raw JSON (when input is already valid JSON)
"example": {"raw": "json"}

String Literal Processing

The example serializer intelligently handles string literals:

/**
 * Checks if a string value is a quoted literal
 * @param value String to check
 * @return true if starts and ends with quotes (single or double)
 */
boolean isStringLiteral(String value);

/**
 * Checks if a string represents valid JSON (not a plain string)
 * Detects objects, arrays, booleans, and numbers
 * @param value String to check
 * @return true if the string is valid JSON
 */
boolean isNotJsonString(String value);

Configuration

Automatic Registration

The module is automatically registered when using the provided Spring configuration:

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    // Swagger2JacksonModule is automatically configured
}

Manual Configuration

For custom ObjectMapper configuration:

@Configuration
public class JacksonConfig {
    
    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        
        // Register Swagger 2 module
        Swagger2JacksonModule swagger2Module = new Swagger2JacksonModule();
        swagger2Module.maybeRegisterModule(mapper);
        
        return mapper;
    }
}

Serialization Settings

The module automatically configures these Jackson settings:

  • SerializationFeature.FAIL_ON_EMPTY_BEANS = false - Prevents errors on empty objects
  • Custom mix-ins for all Swagger 2 model types
  • Special handling for example values and vendor extensions

Supported Types

The module provides custom serialization for these Swagger 2 types:

Core Types

  • Swagger - Root Swagger specification object
  • Info - API metadata information
  • License - License information
  • Contact - Contact information

Operations and Paths

  • Operation - API operation definitions
  • Path - API path definitions
  • Parameter - Operation parameters
  • Response - Operation responses

Models and Properties

  • Model - Data model definitions
  • Property - Model property definitions
  • Xml - XML-specific metadata

Security

  • SecurityRequirement - Security requirements
  • SecuritySchemeDefinition - Security scheme definitions

Documentation

  • Tag - Operation tags for grouping
  • ExternalDocs - External documentation links
  • Scheme - Supported protocols (HTTP, HTTPS)

JSON Output Optimization

The serialization produces optimized JSON output:

  1. Compact Format: No empty or null values
  2. Proper Types: Correct JSON types for all values
  3. Valid Examples: Properly formatted example values
  4. Standard Compliance: Follows Swagger 2.0 specification
  5. Readable Output: Clean, well-formatted JSON structure

Integration Points

The Jackson module integrates with:

  • Spring Boot auto-configuration
  • Springfox documentation generation
  • Swagger UI and other client tools
  • Custom ObjectMapper configurations
  • Third-party JSON processing tools

Install with Tessl CLI

npx tessl i tessl/maven-io-springfox--springfox-swagger2

docs

configuration.md

documentation-generation.md

index.md

json-serialization.md

web-integration.md

tile.json