Code Monkey home page Code Monkey logo

Comments (6)

mp911de avatar mp911de commented on August 12, 2024

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, AttributeEntity is missing so we do not have a complete example here.
You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

from spring-data-commons.

G0dC0der avatar G0dC0der commented on August 12, 2024

A simple demo. Look at class DemoApplication. There is also a docker-compose file, that starta a Mongo instance.
demo.zip

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:

Saved: Attribute[id=65c1423f0fbf892135c39397, format=RecursiveFormat[format=RecursiveFormat[format=RecursiveFormat[format=StaticFormat[format=FOO]]]]]
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"
}
Reading document as Attribute: Attribute[id=65c1423f0fbf892135c39397, format=RecursiveFormat[format=null]]

from spring-data-commons.

G0dC0der avatar G0dC0der commented on August 12, 2024

So, when fetching the object with mongTemplate.execute, the returned object looks perfectly fine. However, when findById is used, it does not!

from spring-data-commons.

christophstrobl avatar christophstrobl commented on August 12, 2024

@G0dC0der thanks for the reproducer - we'll have a look.

from spring-data-commons.

christophstrobl avatar christophstrobl commented on August 12, 2024

So apparently this is an issue when the mapping requires objects to be created via an argument constructor. Things works as expected when switching RecursiveFormat to a class having a noargs constructor.

from spring-data-commons.

christophstrobl avatar christophstrobl commented on August 12, 2024

The issue stems from PersistentEntityParameterValueProvider which considers the constructor Parameter to belong to the parent type and then returning null for the actual value.

from spring-data-commons.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.