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

MongoDB Relationship Expects Object #3277

Open
SenorPez opened this issue Dec 31, 2024 · 2 comments
Open

MongoDB Relationship Expects Object #3277

SenorPez opened this issue Dec 31, 2024 · 2 comments

Comments

@SenorPez
Copy link

Expected Behavior

Relationship defined in the parent POJO:

@MappedEntity("starSystems")

...

    @Relation(value = ONE_TO_MANY, mappedBy = "starSystem")
    @Join("stars")
    private final Set<Star> stars;

Inverse relationship defined in the child POJO:

@MappedEntity("stars")

...

    @NonNull
    @Relation(value = MANY_TO_ONE)
    @Join("starSystems")
    private StarSystem starSystem;

MongoDB document, as described in https://www.mongodb.com/docs/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/#std-label-data-modeling-publisher-and-books. (There's no unbounded array issue as stars in a star system maxes out at 3.) IDs are Int32s.

{
    "_id": 867158537,
    "name":" Home",
    "stars":[
        -1593524705"
    ]
}

It is expected that the Set field of the POJO will be properly constructed from the "stars" collection using a lookup of the IDs in the "stars" array in the "starSystem" document.

Actual Behaviour

Instead, there's an 500 error. StarSystem can not be deserialized:

Caused by: io.micronaut.serde.exceptions.SerdeException: Unexpected token NUMBER, expected START_OBJECT 
 at 
	at io.micronaut.serde.bson.BsonReaderDecoder.createDeserializationException(BsonReaderDecoder.java:312)

Steps To Reproduce

  1. Create Parent and Child POJOs.
  2. Create Mongo documents.
  3. Attempt to read POJOs.

Environment Information

-JDK 21; not using any experimental features.

Example Application

No response

Version

4.4.4

@SenorPez
Copy link
Author

SenorPez commented Jan 1, 2025

Can be worked around by nesting IDs in an object; suggest an improvement to the parser to avoid this extraneous information:

{
  "_id": 867158537,
  "name": "Home",
  "stars": [
    {
      "_id": -1593524705
    }
  ]
}

@dstepanov
Copy link
Contributor

If you have multiple or composite IDs you would need an object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants