Addresses three critical issues with $ref handling in OpenAPI specifications:
1. Validator now properly validates $ref paths - detects invalid ref formats (e.g., OAS 3.x
document using OAS 2.0 ref format like #/definitions/Foo) and non-existent component
references
2. Converter now correctly rewrites $ref paths when converting between OAS versions - transforms
refs from #/definitions/ to #/components/schemas/ (and vice versa) along with parameters,
responses, and security schemes
3. Comprehensive unit test coverage added - 14 new tests ensure these issues won't regress
Validator changes (validator/validator.go):
- Added validateRef() to validate single $ref points to valid component
- Added buildOAS2ValidRefs() and buildOAS3ValidRefs() to build valid ref maps
- Added validateSchemaRefs() for recursive schema ref validation (231 lines)
- Added validateOAS2Refs() and validateOAS3Refs() for document-level validation
- Added 600+ lines of validation logic total
Converter changes (converter/helpers.go, oas2_to_oas3.go, oas3_to_oas2.go):
- Added rewriteRefOAS2ToOAS3() and rewriteRefOAS3ToOAS2() for ref string transformation
- Added rewriteSchemaRefsOAS2ToOAS3/OAS3ToOAS2() for recursive schema ref rewriting (64 lines)
- Added rewriteAllRefsOAS2ToOAS3/OAS3ToOAS2() to traverse entire document
- Added 350+ lines of rewriting logic total
- Fixed parameter Ref field not being copied during conversion
Test coverage:
- validator_test.go: Added TestRefValidation with 10 comprehensive test cases
- converter_test.go: Added 4 test functions for ref rewriting (basic, nested, parameters)
- All existing tests continue to pass ensuring backward compatibility
Reference format mappings:
- OAS 2.0 → 3.x: #/definitions/ → #/components/schemas/
- OAS 2.0 → 3.x: #/parameters/ → #/components/parameters/
- OAS 2.0 → 3.x: #/responses/ → #/components/responses/
- OAS 2.0 → 3.x: #/securityDefinitions/ → #/components/securitySchemes/
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Summary of $ref Validation and Conversion Implementation
All tasks have been completed successfully. Here's a comprehensive summary of the work:
Issues Addressed
Issue 1: Validator fails to validate
$refpaths properly$refvalues using OAS 2.0 format (e.g.,#/definitions/Foo) without any validation errors$refvalidation tovalidator/validator.goIssue 2: Converter fails to update
$refpaths when converting between versionsdefinitionstocomponents.schemas, but refs remained as#/definitions/Foo$refrewriting toconverter/helpers.go,converter/oas2_to_oas3.go, andconverter/oas3_to_oas2.goIssue 3: Unit test coverage failed to identify these issues
$refvalidation or conversionImplementation Details
Validator Package (
validator/validator.go)Added 600+ lines of validation logic:
New Functions:
validateRef()- Validates a single$refpoints to valid componentbuildOAS2ValidRefs()- Builds map of valid OAS 2.0 reference pathsbuildOAS3ValidRefs()- Builds map of valid OAS 3.x reference pathsvalidateSchemaRefs()- Recursively validates all refs in schemas (231 lines)validateParameterRef(),validateResponseRef(),validateRequestBodyRef()- Validate refs in these typesvalidateOAS2Refs()- Validates all refs in OAS 2.0 document (78 lines)validateOAS3Refs()- Validates all refs in OAS 3.x document (168 lines)Test Coverage (
validator/validator_test.go):TestRefValidation()with 10 comprehensive test cases:✅ All 10 test cases pass
Converter Package (
converter/helpers.go,converter/oas2_to_oas3.go,converter/oas3_to_oas2.go)Added 350+ lines of ref rewriting logic:
New Functions:
rewriteRefOAS2ToOAS3()- Rewrites single ref string (2.0 → 3.x format)rewriteRefOAS3ToOAS2()- Rewrites single ref string (3.x → 2.0 format)rewriteSchemaRefsOAS2ToOAS3()- Recursively rewrites all refs in schema (64 lines)rewriteSchemaRefsOAS3ToOAS2()- Recursively rewrites all refs in schema (64 lines)rewriteParameterRefsOAS2ToOAS3/OAS3ToOAS2()- Handles parameter refsrewriteResponseRefsOAS2ToOAS3/OAS3ToOAS2()- Handles response refsrewriteRequestBodyRefsOAS2ToOAS3/OAS3ToOAS2()- Handles request body refsrewritePathItemRefsOAS2ToOAS3/OAS3ToOAS2()- Handles path item refsrewriteAllRefsOAS2ToOAS3()- Traverses entire OAS 3.x document (49 lines)rewriteAllRefsOAS3ToOAS2()- Traverses entire OAS 2.0 document (33 lines)Reference Format Mappings:
OAS 2.0 → OAS 3.x:
#/definitions/→#/components/schemas/#/parameters/→#/components/parameters/#/responses/→#/components/responses/#/securityDefinitions/→#/components/securitySchemes/Bug Fixes:
Reffield not being copied during conversion (in bothconvertOAS2ParameterToOAS3()andconvertOAS3ParameterToOAS2())Test Coverage (
converter/converter_test.go):TestRefRewritingOAS2ToOAS3()- Verifies basic ref rewriting (2.0 → 3.x)TestRefRewritingOAS3ToOAS2()- Verifies basic ref rewriting (3.x → 2.0)TestRefRewritingNestedSchemas()- Verifies nested refs in properties, allOf, anyOf, oneOfTestRefRewritingParameters()- Verifies parameter refs are rewritten✅ All 4 test cases pass
✅ All 19 existing converter tests pass
Joiner Package Review
Finding: No changes needed for joiner package
Reason:
#/components/schemas/#/definitions/Test Results
Validator Package:
Converter Package:
Files Modified
rewriteAllRefsOAS2ToOAS3()rewriteAllRefsOAS3ToOAS2()Verification
All existing functionality remains intact:
The implementation successfully addresses all three identified issues with comprehensive validation, conversion, and test coverage.