Status: Accepted
Date: 2026-02-18
The validator supports both OpenAPI 3.0 and OpenAPI 3.1 specifications through runtime detection and version-specific handling, using different JSON Schema validators (Draft 4 for 3.0, 2020-12 for 3.1) and separate JSON parsing libraries.
OpenAPI 3.0 and OpenAPI 3.1 have significant differences:
JSON Schema Version:
- OpenAPI 3.0: Based on JSON Schema Draft 4 (with extensions)
- OpenAPI 3.1: Fully aligned with JSON Schema 2020-12
Schema Keywords:
- OpenAPI 3.0: Uses
nullable: true(extension keyword) - OpenAPI 3.1: Uses standard
type: ["string", "null"](multi-type arrays)
Exclusive Min/Max:
- OpenAPI 3.0:
exclusiveMinimum: true(boolean) - OpenAPI 3.1:
exclusiveMinimum: 5(numeric, standard JSON Schema)
Parsing:
- OpenAPI 3.0: Uses
io.swagger.v3.core.util.Json.mapper() - OpenAPI 3.1: Uses
io.swagger.v3.core.util.Json31.mapper()
Supporting both versions was critical because:
- Many users have existing OpenAPI 3.0 specs
- OpenAPI 3.1 is the current standard
- Migration between versions is non-trivial
- Breaking compatibility would fragment the user base
- Same validator should work across both versions
Runtime Version Detection:
boolean isOpenApi30 = api.getSpecVersion() == SpecVersion.V30;The validator checks the OpenAPI spec version at initialization and configures:
-
JSON Schema Validator:
- OpenAPI 3.0 →
SpecVersion.VersionFlag.V4withOpenApi30meta-schema - OpenAPI 3.1 →
SpecVersion.VersionFlag.V202012withOpenApi31meta-schema
- OpenAPI 3.0 →
-
JSON Parser:
- OpenAPI 3.0 →
Json.mapper()(standard Jackson mapper) - OpenAPI 3.1 →
Json31.mapper()(3.1-aware Jackson mapper)
- OpenAPI 3.0 →
-
Schema Transformations:
- Version-specific transformers apply based on
isOpenApi30flag - Different handling for nullable, exclusive min/max, type arrays
- Version-specific transformers apply based on
Implementation:
public SchemaValidator(OpenAPI api, MessageResolver messages) {
this.isOpenApi30 = api.getSpecVersion() == SpecVersion.V30;
// Configure JSON Schema factory
SpecVersion.VersionFlag specVersion = isOpenApi30
? SpecVersion.VersionFlag.V4
: SpecVersion.VersionFlag.V202012;
JsonMetaSchema baseMetaSchema = isOpenApi30
? OpenApi30.getInstance()
: OpenApi31.getInstance();
// Use version-appropriate JSON mapper
ObjectMapper mapper = isOpenApi30 ? Json.mapper() : Json31.mapper();
// Transformers adapt behavior based on isOpenApi30 flag
}This approach provides:
- Transparent Compatibility - Same API works for both versions
- Correct Validation - Each version validated against its JSON Schema spec
- No User Action Required - Version detected automatically from spec
- Version-Specific Features - Each version gets proper JSON Schema semantics
- Future-Proof - Easy to add new versions with same pattern
- No Breaking Changes - Existing 3.0 specs continue working
-
Separate Validators - Different classes for OpenAPI 3.0 and 3.1
- ❌ Duplicates validation logic
- ❌ Forces users to know version and choose correct validator
- ❌ Harder to maintain consistency
-
Convert 3.0 to 3.1 - Automatically upgrade specs to 3.1
- ❌ Risky automated conversion (semantic differences)
- ❌ Users may not want spec modified
- ❌ Loses original spec for error reporting
-
Support Only Latest - Drop OpenAPI 3.0 support
- ❌ Breaks existing users
- ❌ Forces migration before validation can work
- ❌ Not backwards compatible
-
Manual Version Configuration - User specifies version
- ❌ Error-prone (version mismatch between config and spec)
- ❌ Adds configuration complexity
- ❌ Spec already contains version information
- Seamless support for both OpenAPI 3.0 and 3.1
- Users don't need to know or configure version
- Correct JSON Schema validation for each version
- Future versions can be added with same pattern
- No breaking changes for existing users
- Version-specific features work correctly (nullable, multi-type, etc.)
- Slightly more complex initialization logic
- Two JSON mappers and JSON Schema factories in memory
- Version detection adds small initialization overhead
- Must maintain version-specific code paths
- Testing requires covering both versions
For Library Developers:
- Always check
isOpenApi30flag when version-specific behavior needed - Use appropriate JSON mapper:
isOpenApi30 ? Json.mapper() : Json31.mapper() - Test features against both OpenAPI 3.0 and 3.1 specs
- Schema transformers must handle version differences
- Keep up with changes in both JSON Schema Draft 4 and 2020-12
For Library Users:
- No action required - version detected automatically
- Same
OpenApiInteractionValidatorAPI for both versions - Validation behavior matches OpenAPI version semantics
- Error messages reference correct spec version
Version-Specific Transformations:
if (isOpenApi30) {
// Apply OpenAPI 3.0 transformations (e.g., exclusive min/max conversion)
} else {
// Apply OpenAPI 3.1 transformations (e.g., multi-type support)
}OpenAPI 3.1 introduced support for type as an array:
type: ["string", "null"] # OpenAPI 3.1Instead of OpenAPI 3.0's nullable extension:
type: string
nullable: true # OpenAPI 3.0The validator handles this through:
hasMultipartTypeSchema(): Detects multi-type schemasvalidateMultiTypeSchema(): Validates against each type, succeeds on first match- Version-appropriate JSON Schema validator understands semantics
SchemaValidator.java: Version detection and configuration (lines 82-166)- OpenAPI 3.0 meta-schema:
com.networknt.schema.oas.OpenApi30 - OpenAPI 3.1 meta-schema:
com.networknt.schema.oas.OpenApi31 - Multi-type validation:
SchemaValidator.validateMultiTypeSchema() - Test specs:
openapi-request-validator-core/src/test/resources/oai/v3/andoai/v31/