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

Using Kotlin BsonProperty is ignored when persisting a document #8188

Closed
ekans opened this issue Mar 26, 2020 · 1 comment
Closed

Using Kotlin BsonProperty is ignored when persisting a document #8188

ekans opened this issue Mar 26, 2020 · 1 comment
Labels
area/kotlin kind/bug Something isn't working triage/invalid This doesn't seem right

Comments

@ekans
Copy link

ekans commented Mar 26, 2020

Describe the bug
Using Kotlin I use BsonProperty on a field and the key name seems ignored when the entity is persisted but BsonProperty value is used when reading the document.

The initial discussion on Zulip

Expected behavior
The BsonProperty value is used when the document is persisted.

Actual behavior
The BsonProperty value is not ignored.

To Reproduce
Here is a reproducer
The relevant part:

@MongoEntity(collection = "person")
data class Person @BsonCreator constructor(
  @BsonProperty("first_name") var name: String
) : PanacheMongoEntityBase()

@ApplicationScoped
class PersonRepository : PanacheMongoRepository<Person>

@QuarkusTest
class PersonResourceTest {

  @Inject
  lateinit var personRepository: PersonRepository

  @Test
  fun testHelloEndpoint() {
    personRepository.deleteAll()
    personRepository.persist(Person("toto"))
    personRepository.listAll() // <-- failed here
  }
}
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.864 s <<< FAILURE! - in org.acme.mongodb.panache.PersonResourceTest
[ERROR] testHelloEndpoint  Time elapsed: 1.204 s  <<< ERROR!
org.bson.codecs.configuration.CodecConfigurationException:
An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'Person' failed with the following exception:

Could not construct new instance of: Person. Missing the following properties: [first_name]

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
        at org.acme.mongodb.panache.PersonResourceTest.testHelloEndpoint(PersonResourceTest.kt:31)
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Could not construct new instance of: Person. Missing the following properties: [first_name]
        at org.acme.mongodb.panache.PersonResourceTest.testHelloEndpoint(PersonResourceTest.kt:31)
Caused by: org.bson.codecs.configuration.CodecConfigurationException
        at org.acme.mongodb.panache.PersonResourceTest.testHelloEndpoint(PersonResourceTest.kt:31)
Caused by: org.bson.codecs.configuration.CodecConfigurationException
        at org.acme.mongodb.panache.PersonResourceTest.testHelloEndpoint(PersonResourceTest.kt:31)
Caused by: java.lang.reflect.InvocationTargetException
        at org.acme.mongodb.panache.PersonResourceTest.testHelloEndpoint(PersonResourceTest.kt:31)
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method org.acme.mongodb.panache.Person.<init>, parameter name
        at org.acme.mongodb.panache.PersonResourceTest.testHelloEndpoint(PersonResourceTest.kt:31)

Configuration

quarkus.mongodb.connection-string = mongodb://127.0.0.1:27056
quarkus.mongodb.database = reproducer
@ekans ekans added the kind/bug Something isn't working label Mar 26, 2020
@geoand
Copy link
Contributor

geoand commented Mar 26, 2020

This is not a Quarkus issue, but an issue with the application itself.

The proper way to use the data class of the reproducer:

@MongoEntity(collection = "person")
data class Person @BsonCreator constructor(
        @get:BsonProperty("first_name") @param:BsonProperty("first_name") var name: String
) : PanacheMongoEntityBase()

@geoand geoand closed this as completed Mar 26, 2020
@geoand geoand added the triage/invalid This doesn't seem right label Mar 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kotlin kind/bug Something isn't working triage/invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants