Maintaining Backward Compatibility in the nj-dialer-routing-service
In the nj-dialer-routing-service, we recently addressed a critical issue regarding how SIP endpoints are resolved and rewritten. Our goal was to improve prefix handling for specific routing scenarios without breaking existing scalar or legacy path integrations that rely on direct endpoint handling.
The Problem
Initially, our routing logic was unconditionally rewriting the destination URI by injecting a prefix into the user part of the SIP URI. While this worked for endpoints requiring specific routing prefixes, it caused unexpected changes for the legacy scalar path, which expects a bare gateway URI. This mismatch resulted in broken R-URI structures for consumers relying on the original endpoint format.
The Solution: Conditional Rewriting
To maintain backward compatibility, we refactored the resolution logic to verify the presence of a prefix before applying any modifications. The resolveEndpoints function now differentiates between two states:
- Scalar/Legacy path: Returns the original, untouched gateway URI.
- Prefix-enabled path: Returns the full, reconstructed SIP R-URI including the prefix and digits.
This separation ensures that legacy integrations remain unaffected while new functionality benefits from the enhanced URI formatting.
Illustrative Implementation
Here is how we represent the decision logic for resolving SIP endpoints in Go:
func resolveEndpoints(gateway string, prefix string, digits string) string {
// If no prefix is present, return the original gateway URI
if prefix == "" {
return gateway
}
// Otherwise, construct the full SIP URI with the prefix
return fmt.Sprintf("sip:%s%s@%s", prefix, digits, gateway)
}
Impact and Testing
By implementing this conditional check, we restored the contract tests for our routing plans. We also added a regression test to ensure that the scalar path specifically never receives a rewritten URI. This change highlights the importance of protecting legacy behavior when introducing new routing enhancements in a distributed system.