Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map incoming company properties request to value object #1

Open
sitole opened this issue Apr 19, 2022 · 1 comment
Open

Map incoming company properties request to value object #1

sitole opened this issue Apr 19, 2022 · 1 comment
Assignees
Labels
feature New feature or request help Extra attention is needed

Comments

@sitole
Copy link
Contributor

sitole commented Apr 19, 2022

Introduction

Right now API properties attribute in company response (https://github.com/goforboom/hubspot/blob/main/hubspot/src/main/kotlin/com/goforboom/hubspot/domain/company/Company.kt) is mapped just to dummy Map<String, Any> object but would be great to map it to custom value object (same in requests).

Issue description

There is some trouble with Generic and Jackson mapping because FasterXML/jackson-databind#921 and LinkedHashMap because Java lost Generic information in runtime and Jackson is no able to map incoming request property to right type. I partially fix this issue in our Fakturoid implementation (https://github.com/goforboom/fakturoid/blob/1dd7dfbcbd9e4b6166c1965ce38aed58435b33c3/fakturoid/src/main/kotlin/com/goforboom/fakturoid/model/mapper/Mapper.kt#L20) with TypeReference<R> but in current implementation problem is deeper, we want to transfer type with generic in generic:

class Company<P>(
    val id: Int,
    val props: P
)

Any help is welcome! 🙌🏼

@sitole sitole added feature New feature or request help Extra attention is needed labels Apr 19, 2022
@sitole sitole self-assigned this Apr 19, 2022
@crmolinaz
Copy link

crmolinaz commented Nov 6, 2023

@sitole I think for that you will need something like:

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper

class Company<P>(
    val id: Int,
    val props: P
) {
    companion object {
        inline fun <reified P> fromJson(json: String, mapper: ObjectMapper): Company<P> {
            // This creates a type reference based on the provided type P.
            val typeRef: TypeReference<Company<P>> = object : TypeReference<Company<P>>() {}

            // Use the mapper to read the value with the type information.
            return mapper.readValue(json, typeRef)
        }
    }
}

and then use it like:
val company: Company<CustomCompanyProperties> = Company.fromJson(json, objectMapper)

if you want, you can include the mapper inside the Company, like that will be only necessary the json as parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request help Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants