Skip to content

Commit

Permalink
Fix #250
Browse files Browse the repository at this point in the history
- Updated the `body` function in `Pact.kt` to use a safer casting approach with `as?` instead of inline reified type parameters, improving type safety.
- Modified the `serializerWith` and `serializerAsDefault` functions in `Utils.kt` to remove inline reified type parameters, simplifying the function signatures while maintaining functionality.

These changes enhance type handling and simplify the serialization utility methods in the pact-jvm-mock library.
  • Loading branch information
ludorival committed Jan 9, 2025
1 parent 6d10182 commit 7a1dffa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ data class Pact(
) {

@JsonIgnore
inline fun <reified T> body() : T? = if (body is T) body else null
@Suppress("UNCHECKED_CAST")
fun <T> body() : T? = body as? T
enum class Method {
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializerProvider

inline fun <reified T> serializerWith(crossinline supplier: (JsonGenerator) -> Unit) = object : JsonSerializer<T>() {
fun <T> serializerWith( supplier: (JsonGenerator) -> Unit) = object : JsonSerializer<T>() {
override fun serialize(value: T, gen: JsonGenerator, serializers: SerializerProvider?) {
supplier(gen)
}
}

inline fun <reified T> serializerAsDefault(defaultValue: String) =
fun < T> serializerAsDefault(defaultValue: String) =
serializerWith<T> { it.writeString(defaultValue) }


Expand Down

0 comments on commit 7a1dffa

Please sign in to comment.