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

Query annotation with $oid leads to a parse error [DATAMONGO-1070] #1991

Closed
spring-projects-issues opened this issue Oct 9, 2014 · 11 comments
Assignees
Labels
type: bug A general bug

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Oct 9, 2014

Ramnivas Laddad opened DATAMONGO-1070 and commented

When a query that uses $oid along with a parameter placeholder is written, it leads to a parse error during application load time (This used to work fine in version 1.3.2, but is broken in at least 1.5.4-1.7.x):

(Reproducible project attached)

@Query("{ 'organization': {'$ref': 'organizations', '$id': { '$oid': ?0 } } }")
List<Person> findByOrganization(String organizationId);
Caused by: java.lang.IllegalArgumentException: invalid ObjectId [_param_0]
	at org.bson.types.ObjectId.<init>(ObjectId.java:181)
	at org.bson.types.ObjectId.<init>(ObjectId.java:167)
	at com.mongodb.util.JSONCallback.objectDone(JSONCallback.java:71)
	at com.mongodb.util.JSONParser.parseObject(JSON.java:261)
	at com.mongodb.util.JSONParser.parse(JSON.java:213)
	at com.mongodb.util.JSONParser.parseObject(JSON.java:249)
	at com.mongodb.util.JSONParser.parse(JSON.java:213)
	at com.mongodb.util.JSONParser.parseObject(JSON.java:249)
	at com.mongodb.util.JSONParser.parse(JSON.java:213)
	at com.mongodb.util.JSONParser.parse(JSON.java:163)
	at com.mongodb.util.JSON.parse(JSON.java:99)
	at com.mongodb.util.JSON.parse(JSON.java:79)
	at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery$ParameterBindingParser.parseParameterBindingsFrom(StringBasedMongoQuery.java:214)
	at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.<init>(StringBasedMongoQuery.java:76)
...

Affects: 1.5.4 (Dijkstra SR4), 1.7 M1 (Fowler)

Attachments:

Issue Links:

Backported to: 1.6.1 (Evans SR1)

0 votes, 5 watchers

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 10, 2014

Oliver Drotbohm commented

Simply using:

@Query("{ 'organization': {'$ref': 'organizations', '$id': ?0 } }")

should work. Actually the code should even work without an @Query if you change the method name to findByOrganizationId

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 10, 2014

Ramnivas Laddad commented

Unfortunately, neither of the suggested variations work. The @Query version leads to a similar parsing error, whereas findByOrganizationId without @Query finds an empty collection.

(The attached project is updated to illustrate these changes)

BTW, in mongoshell the query that works is:

db.persons.find({ 'organization': {'$ref': 'organizations', '$id': ObjectId("543801b330041bd9621a78be") } } )

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 14, 2014

Ramnivas Laddad commented

Feedback provided as a comment (I wasn't aware of the "Provide Feedback for Developer" button)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 14, 2014

Oliver Drotbohm commented

Just a quick note as I am still having to polish the fixes. There's a couple of minor glitches that need to be fixed. In the meantime you should be able to get this working by removing @Query entirely and changing the query method to:

List<Person> findByOrganization(Organization organization);

This will generate the correct query. I'll push a fix for both the derived case (using …OrganizationId(String od)) as well as the case for the parameter binding for a manually defined query

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 17, 2014

Oliver Drotbohm commented

Pushed to both the master and bugfix branch. CI snapshots available for the bugfix one. Still working on getting the CI server to build the master branch due to some infrastructure glitches

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 19, 2014

Ramnivas Laddad commented

Thanks, Oliver. I tested with a locally built version and it works fine with findByOrganizationId without any @Query and with

@Query("{ 'organization': {'$ref': 'organizations', '$id': ?0 } }")

BTW,

@Query("{ 'organization': {'$ref': 'organizations', '$id': { '$oid': ?0 } } }") 

still gives the parse error, but since I have two other ways to get the same result, it is not important to me (but might be to you to check what should have been semantics for such a query).

Any ETA on when the artifacts from the master branch will be available (and where is the snapshot for the 'bugfix' branch available?)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 27, 2018

eacdy commented

Ramnivas Laddad Oliver Drotbohm

Same issue in Spring Boot 2.0.6 and Spring Boot 2.1.0.

Could you please reopen this issue?

 

Code is like below:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document
// 如果不指定,那就是包名全路径
@TypeAlias("company_xx")
public class Company {
  @Id
  private String id;
  private String name;
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document
public class User {
  /**
   * ID
   */
  @Id
  private String id;

  /**
   * 用户名
   */
  private String username;

  /**
   * 密码
   */
  private String password;

  @DBRef
  private Company company;
}

/**
 * @author zhouli
 */
@Repository
public interface UserRepository extends ReactiveCrudRepository<User, String> {
  // TODO why this causes error?
  @Query("{'company' :{'$ref' : 'company' , '$oid' : ?0}}")
  Mono<User> findByCompanyId(String companyId);

  // this is ok.
  Mono<User> findByCompany(Company company);
  
  // TODO why this can never return ??
  @Query("{'company.$id': ?0}")
  Mono<User> findByCompanyId2(String companyId);
}

The code Can never start up but return a error message like below:

Caused by: java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [_param_0]Caused by: java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [_param_0] at org.bson.types.ObjectId.parseHexString(ObjectId.java:550) ~[bson-3.8.2.jar:na] at org.bson.types.ObjectId.<init>(ObjectId.java:239) ~[bson-3.8.2.jar:na] at com.mongodb.util.JSONCallback.objectDone(JSONCallback.java:88) ~[mongodb-driver-3.8.2.jar:na] at com.mongodb.util.JSONParser.parseObject(JSON.java:275) ~[mongodb-driver-3.8.2.jar:na] at com.mongodb.util.JSONParser.parse(JSON.java:228) ~[mongodb-driver-3.8.2.jar:na] at com.mongodb.util.JSONParser.parseObject(JSON.java:264) ~[mongodb-driver-3.8.2.jar:na] at com.mongodb.util.JSONParser.parse(JSON.java:228) ~[mongodb-driver-3.8.2.jar:na] at com.mongodb.util.JSONParser.parse(JSON.java:156) ~[mongodb-driver-3.8.2.jar:na]

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 27, 2018

Mark Paluch commented

This ticket is shipped with earlier releases and cannot be reopened

Please use the format:

@Query("{'company': {'$ref': 'company', '$id': ?0 } }")

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 28, 2018

eacdy commented

Mark Paluch 

@Query("{'company': {'$ref': 'company', '$id': ?0 } }")

The projects Can start up, but quey returns nothing

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 28, 2018

Mark Paluch commented

Please file a new ticket so we can continue the discussion there instead of cluttering the history of this one

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 7, 2019

Zeyar Htike commented

// code placeholder
Query("{'organization':{'$ref':'organization','$id': ?0}}") 
Mono<User> findByOrganizationId(org.bson.types.ObjectId id);

 

You need to parse ObjectId instead of String from client code.

// code placeholder
String companyId="company_id";
repo.findByOrganizationId(new ObjectId(companyId));

 

 

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

No branches or pull requests

2 participants