@ -24,6 +24,7 @@ import jakarta.servlet.http.HttpServletRequest;
import org.jspecify.annotations.Nullable ;
import org.jspecify.annotations.Nullable ;
import org.springframework.util.Assert ;
import org.springframework.util.Assert ;
import org.springframework.util.StringUtils ;
import org.springframework.web.accept.ApiVersionStrategy ;
import org.springframework.web.accept.ApiVersionStrategy ;
import org.springframework.web.accept.InvalidApiVersionException ;
import org.springframework.web.accept.InvalidApiVersionException ;
import org.springframework.web.accept.NotAcceptableApiVersionException ;
import org.springframework.web.accept.NotAcceptableApiVersionException ;
@ -57,20 +58,24 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers
private final Set < String > content ;
private final Set < String > content ;
public VersionRequestCondition ( ) {
/ * *
this . versionValue = null ;
* Constructor with the version , if set on the { @code @RequestMapping } , and
this . version = null ;
* the { @code ApiVersionStrategy } , if API versioning is enabled .
this . baselineVersion = false ;
* /
this . versionStrategy = NO_OP_VERSION_STRATEGY ;
public VersionRequestCondition ( @Nullable String version , @Nullable ApiVersionStrategy strategy ) {
this . content = Collections . emptySet ( ) ;
this . versionStrategy = ( strategy ! = null ? strategy : NO_OP_VERSION_STRATEGY ) ;
}
if ( StringUtils . hasText ( version ) ) {
this . baselineVersion = version . endsWith ( "+" ) ;
public VersionRequestCondition ( String configuredVersion , ApiVersionStrategy versionStrategy ) {
this . versionValue = updateVersion ( version , this . baselineVersion ) ;
this . baselineVersion = configuredVersion . endsWith ( "+" ) ;
this . version = this . versionStrategy . parseVersion ( this . versionValue ) ;
this . versionValue = updateVersion ( configuredVersion , this . baselineVersion ) ;
this . content = Set . of ( version ) ;
this . version = versionStrategy . parseVersion ( this . versionValue ) ;
}
this . versionStrategy = versionStrategy ;
else {
this . content = Set . of ( configuredVersion ) ;
this . versionValue = null ;
this . version = null ;
this . baselineVersion = false ;
this . content = Collections . emptySet ( ) ;
}
}
}
private static String updateVersion ( String version , boolean baselineVersion ) {
private static String updateVersion ( String version , boolean baselineVersion ) {
@ -103,23 +108,23 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers
return this ;
return this ;
}
}
Comparable < ? > v ersion = ( Comparable < ? > ) request . getAttribute ( VERSION_ATTRIBUTE_NAME ) ;
Comparable < ? > requestV ersion = ( Comparable < ? > ) request . getAttribute ( VERSION_ATTRIBUTE_NAME ) ;
if ( v ersion = = null ) {
if ( requestV ersion = = null ) {
String value = this . versionStrategy . resolveVersion ( request ) ;
String value = this . versionStrategy . resolveVersion ( request ) ;
v ersion = ( value ! = null ? parseVersion ( value ) : this . versionStrategy . getDefaultVersion ( ) ) ;
requestV ersion = ( value ! = null ? parseVersion ( value ) : this . versionStrategy . getDefaultVersion ( ) ) ;
this . versionStrategy . validateVersion ( v ersion, request ) ;
this . versionStrategy . validateVersion ( requestV ersion, request ) ;
version = ( v ersion ! = null ? v ersion : NO_VERSION_ATTRIBUTE ) ;
requestVersion = ( requestV ersion ! = null ? requestV ersion : NO_VERSION_ATTRIBUTE ) ;
request . setAttribute ( VERSION_ATTRIBUTE_NAME , ( v ersion) ) ;
request . setAttribute ( VERSION_ATTRIBUTE_NAME , ( requestV ersion) ) ;
}
}
if ( v ersion = = NO_VERSION_ATTRIBUTE ) {
if ( requestV ersion = = NO_VERSION_ATTRIBUTE ) {
return this ;
return this ;
}
}
// At this stage, match all versions as baseline versions.
// At this stage, match all versions as baseline versions.
// Strict matching for fixed versions is enforced at the end in handleMatch.
// Strict matching for fixed versions is enforced at the end in handleMatch.
int result = compareVersions ( this . version , v ersion) ;
int result = compareVersions ( this . version , requestV ersion) ;
return ( result < = 0 ? this : null ) ;
return ( result < = 0 ? this : null ) ;
}
}