-
Notifications
You must be signed in to change notification settings - Fork 681
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
Invalid parent parameter provided to static inner class constructors #3038
Comments
If you would like us to spend some time helping you to diagnose the problem, please spend some time describing it and, ideally, providing a minimal yet complete sample that reproduces the problem. Looking at the example, |
A simple demo. Look at class DemoApplication. There is also a docker-compose file, that starta a Mongo instance. package com.example.demo;
import com.example.demo.DemoApplication.Format.RecursiveFormat;
import com.example.demo.DemoApplication.Format.StaticFormat;
import java.util.Random;
import org.bson.json.JsonWriterSettings;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Document("attributes")
public record Attribute(@Id ObjectId id, Format format) {}
public sealed interface Format {
public record RecursiveFormat(Format format) implements Format {}
public record StaticFormat(String format) implements Format {}
}
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void run(final String... args) throws Exception {
var id = new ObjectId();
var attribute = new Attribute(id, new RecursiveFormat(new RecursiveFormat(new RecursiveFormat(new StaticFormat("FOO")))));
mongoTemplate.save(attribute);
System.out.println("Saved: " + attribute);
var document = mongoTemplate.execute("attributes", collection -> collection.find(new org.bson.Document("_id", id)).first());
System.out.println("Reading document as a org.bson.Document" + document.toJson(JsonWriterSettings.builder().indent(true).build()));
var readAttribute = mongoTemplate.findById(id, Attribute.class);
System.out.println("Reading document as Attribute: " + readAttribute);
}
} Logs:
Reading document as a org.bson.Document{
"_id": {
"$oid": "65c14305757b715020f450cd"
},
"format": {
"format": {
"format": {
"format": {
"format": "FOO",
"_class": "com.example.demo.DemoApplication$Format$StaticFormat"
},
"_class": "com.example.demo.DemoApplication$Format$RecursiveFormat"
},
"_class": "com.example.demo.DemoApplication$Format$RecursiveFormat"
},
"_class": "com.example.demo.DemoApplication$Format$RecursiveFormat"
},
"_class": "com.example.demo.DemoApplication$Attribute"
}
|
So, when fetching the object with |
@G0dC0der thanks for the reproducer - we'll have a look. |
So apparently this is an issue when the mapping requires objects to be created via an argument constructor. Things works as expected when switching |
The issue stems from |
This is a document in collection, exported via MongoDB Compass:
At this point, everything looks correct. However, when I read it,
storageFormat.elementFormat
is null.This is the class:
I have temporarily solved this by registering a custom read converter, but I feel like this shouldn't be necessary. I feel like this is a bug in the framework.
I am using Spring Boot 3.2.1.
The text was updated successfully, but these errors were encountered: