@@ -93,35 +93,50 @@ export const contentMediaTypeMappingResolver: MappingResolver<string> = (before,
9393
9494 const beforeKeys = objectKeys ( before )
9595 const afterKeys = objectKeys ( after )
96-
97- // Extract base media types (without parameters like charset, etc.)
98- const beforeBaseTypes = beforeKeys . map ( ( key ) => key . split ( ';' ) [ 0 ] ?? '' )
99- const afterBaseTypes = afterKeys . map ( ( key ) => key . split ( ';' ) [ 0 ] ?? '' )
10096
10197 const unmappedAfterIndices = new Set ( afterKeys . keys ( ) )
10298 const unmappedBeforeIndices = new Set ( beforeKeys . keys ( ) )
10399
104- // Map exact matches first
105- for ( let i = 0 ; i < beforeKeys . length ; i ++ ) {
106- const beforeBaseType = beforeBaseTypes [ i ]
107- const afterIndex = afterBaseTypes . findIndex ( ( afterBaseType , index ) =>
108- afterBaseType === beforeBaseType && unmappedAfterIndices . has ( index )
109- )
100+ function mapExactMatches (
101+ getComparisonKey : ( key : string ) => string
102+ ) : void {
103+
104+ for ( const beforeIndex of unmappedBeforeIndices ) {
105+ const beforeKey = getComparisonKey ( beforeKeys [ beforeIndex ] )
106+
107+ // Find matching after index by iterating over the after indices set
108+ let matchingAfterIndex : number | undefined
109+ for ( const afterIndex of unmappedAfterIndices ) {
110+ const afterKey = getComparisonKey ( afterKeys [ afterIndex ] )
111+ if ( afterKey === beforeKey ) {
112+ matchingAfterIndex = afterIndex
113+ break
114+ }
115+ }
110116
111- if ( afterIndex >= 0 ) {
112- // Exact match found - map it
113- result . mapped [ beforeKeys [ i ] ] = afterKeys [ afterIndex ]
114- unmappedAfterIndices . delete ( afterIndex )
115- unmappedBeforeIndices . delete ( i )
117+ if ( matchingAfterIndex !== undefined ) {
118+ // Match found - create mapping and remove from unmapped sets
119+ result . mapped [ beforeKeys [ beforeIndex ] ] = afterKeys [ matchingAfterIndex ]
120+ unmappedAfterIndices . delete ( matchingAfterIndex )
121+ unmappedBeforeIndices . delete ( beforeIndex )
122+ }
116123 }
117124 }
118125
126+ // First, map exact matches for full media type
127+ mapExactMatches ( ( key ) => key )
128+
129+ // After that, try to map media types by base type for remaining unmapped keys
130+ mapExactMatches ( getMediaTypeBase )
131+
119132 // If exactly one unmapped item in both before and after, try wildcard matching
120133 if ( unmappedBeforeIndices . size === 1 && unmappedAfterIndices . size === 1 ) {
121134 const beforeIndex = Array . from ( unmappedBeforeIndices ) [ 0 ]
122135 const afterIndex = Array . from ( unmappedAfterIndices ) [ 0 ]
123- const beforeBaseType = beforeBaseTypes [ beforeIndex ]
124- const afterBaseType = afterBaseTypes [ afterIndex ]
136+ const beforeKey = beforeKeys [ beforeIndex ]
137+ const afterKey = afterKeys [ afterIndex ]
138+ const beforeBaseType = getMediaTypeBase ( beforeKey )
139+ const afterBaseType = getMediaTypeBase ( afterKey )
125140
126141 // Check if they are compatible using wildcard matching
127142 if ( isWildcardCompatible ( beforeBaseType , afterBaseType ) ) {
@@ -139,6 +154,10 @@ export const contentMediaTypeMappingResolver: MappingResolver<string> = (before,
139154 return result
140155}
141156
157+ function getMediaTypeBase ( mediaType : string ) : string {
158+ return mediaType . split ( ';' ) [ 0 ] ?? ''
159+ }
160+
142161function isWildcardCompatible ( beforeType : string , afterType : string ) : boolean {
143162 const [ beforeMainType , beforeSubType ] = beforeType . split ( '/' )
144163 const [ afterMainType , afterSubType ] = afterType . split ( '/' )
0 commit comments