SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

Obtain doc-id for POST which creates new document within Hook #252

Closed visualist closed 6 years ago

visualist commented 7 years ago

Hi guys, me again. We've got Restheart working for us in production for all but this use case. Thanks for making such an excellent production! I'm running version 3.1.3.

I'm looking for the best/recommended way to obtain the ID of a newly-created document from within the hook() method (implements Hook) from this scenario:

I've tried various ways with inconsistent results, for example:

I was hoping there was a straightforward way to get ID & I'm must missing something. Thanks.

ujibang commented 7 years ago

With POST, the id of the created document is returned in the Location response header.

visualist commented 7 years ago

hmm.. I do need the ID in the hook addon, in Java. I don't see a way to get at the response headers from the HttpServerExchange exchange object passed into the hook() method. As far as response data goes, all I see is the getResponseCode() method on that object.

ujibang commented 7 years ago

Sorry i didn't get your question.

The object returned by context.getDbOperationResult().getNewData() should contain your _id

ujibang commented 7 years ago

That is true of course only for write requests.

You can have a look at the SnooperHook example hook.

visualist commented 7 years ago

Thanks! I didn't think to look at the SnooperHook example.

dbOperationResult has a getNewId() method on it, but right now seems to be always null. Is that the future?

visualist commented 7 years ago

For future reference, here's basically what I did, without guards for NPEs etc: String newId = newData.get("_id").asString().getValue();

ujibang commented 7 years ago

Hi,

The getNewId() is supposed to work.

It is always null but when a new I'd is generated via a POST request.

Can you try? In case we mark this as a bug.

visualist commented 7 years ago

Yes, I will test this out and get back to you.

visualist commented 7 years ago

It is null for a POST to a collection. I snapped a copy of IntelliJ debug window, stopped from within my hook implementation. Look at the top line for getNewId(). The docid at the bottom is my own variable, obtained from digging into newData instead. image

visualist commented 7 years ago

Also, I thought I was running 3.1.3 but I think I'm on a newer revision. My local repo is at: 101de1e7be4417b72d7c4eb2dd2430c19fc4bd9d MongoDB version 3.4.9

visualist commented 7 years ago

I pulled the latest from master, now I'm at 5cd6059017f178aee4b2a6ec69187dde00126ec6 but I'm still seeing the same null result for getNewId() method.

ujibang commented 7 years ago

That's definitely a bug. I m going to fix it.

Thanks

mkjsix commented 6 years ago

@ujibang moving this to 3.2.1 ?

mkjsix commented 6 years ago

@ujibang is there a JIRA issue for this?

ujibang commented 6 years ago

fixed in 842b2166534a581e81776339d64ace319e7bdb1a