-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix NPE when
RequestSerializerFactory
has no ConversionService conf…
…igured. (#82)
- Loading branch information
Showing
3 changed files
with
65 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...-jpa-api/src/main/java/io/vigier/cursorpaging/jpa/serializer/SimpleConversionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.vigier.cursorpaging.jpa.serializer; | ||
|
||
import java.time.Instant; | ||
import org.springframework.core.convert.ConversionService; | ||
import org.springframework.core.convert.TypeDescriptor; | ||
|
||
/** | ||
* Very simple conversion service which might help to do create some test without the need of setting up a more | ||
* sophisticated conversion service, but is not meant to be used in production. | ||
*/ | ||
class SimpleConversionService implements ConversionService { | ||
@Override | ||
public boolean canConvert( final Class<?> sourceType, final Class<?> targetType ) { | ||
if ( sourceType == null || targetType == null ) { | ||
return false; | ||
} | ||
return sourceType.isAssignableFrom( String.class ) && (targetType.isAssignableFrom( Long.class ) | ||
|| targetType.isAssignableFrom( Integer.class ) || targetType.isAssignableFrom( Boolean.class ) | ||
|| targetType.isAssignableFrom( String.class ) || targetType.isAssignableFrom( Object.class )); | ||
} | ||
|
||
@Override | ||
public boolean canConvert( final TypeDescriptor sourceType, final TypeDescriptor targetType ) { | ||
if ( sourceType == null || targetType == null ) { | ||
return false; | ||
} | ||
return canConvert( sourceType.getType(), targetType.getType() ); | ||
} | ||
|
||
@Override | ||
public <T> T convert( final Object source, final Class<T> targetType ) { | ||
if ( targetType == String.class ) { | ||
return targetType.cast( source.toString() ); | ||
} | ||
if ( targetType == Integer.class ) { | ||
return targetType.cast( Integer.valueOf( source.toString() ) ); | ||
} | ||
if ( targetType == Long.class ) { | ||
return targetType.cast( Long.valueOf( source.toString() ) ); | ||
} | ||
if ( targetType == Boolean.class ) { | ||
return targetType.cast( "true".equals( source.toString() ) ); | ||
} | ||
if ( targetType == Instant.class ) { | ||
return targetType.cast( Instant.parse( source.toString() ) ); | ||
} | ||
if ( targetType == Object.class ) { | ||
return targetType.cast( source ); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object convert( final Object source, final TypeDescriptor sourceType, final TypeDescriptor targetType ) { | ||
return convert( source, targetType.getObjectType() ); | ||
} | ||
} |