@@ -39,17 +39,13 @@ function _contextid(contextURI) {
3939 */
4040const idNegotiation = function ( resBody ) {
4141 if ( ! resBody ) return
42- const providedContext = resBody [ "@context" ]
43- if ( ! providedContext ) return resBody
44-
42+ if ( ! resBody [ "@context" ] ) return resBody
4543 let modifiedResBody = JSON . parse ( JSON . stringify ( resBody ) )
46- console . log ( "Negotiate on this" )
47- console . log ( modifiedResBody )
48- if ( _contextid ( providedContext ) ) {
49- modifiedResBody [ "id" ] = modifiedResBody [ "@id" ]
44+ if ( _contextid ( resBody [ "@context" ] ) ) {
45+ modifiedResBody . id = modifiedResBody [ "@id" ]
5046 delete modifiedResBody [ "@id" ]
5147 }
52- delete modifiedResBody [ " _id" ]
48+ delete modifiedResBody . _id
5349 return modifiedResBody
5450}
5551
@@ -107,16 +103,16 @@ const create = async function (req, res, next) {
107103 let provided = JSON . parse ( JSON . stringify ( req . body ) )
108104 let rerumProp = { "__rerum" : utils . configureRerumOptions ( generatorAgent , provided , false , false ) [ "__rerum" ] }
109105 rerumProp . __rerum . slug = slug
110- const providedID = provided [ "_id" ]
111- const providedContext = provided [ "@context" ]
106+ const providedID = provided . _id
112107 let _id = isValidID ( providedID ) ? providedID : ObjectID ( )
113108 delete provided [ "_rerum" ]
114109 delete provided [ "@id" ]
115- delete provided [ "@context" ]
116- if ( _contextid ( providedContext ) ) {
110+ if ( _contextid ( provided [ "@context" ] ) ) {
117111 // id is also protected in this case, so it can't be set.
118- delete provided [ "id" ]
112+ delete provided . id
119113 }
114+ delete provided [ "@context" ]
115+
120116 let newObject = Object . assign ( context , { "@id" : process . env . RERUM_ID_PREFIX + _id } , provided , rerumProp , { "_id" : _id } )
121117 console . log ( "CREATE" )
122118 try {
@@ -270,12 +266,16 @@ const putUpdate = async function (req, res, next) {
270266 }
271267 else {
272268 id = ObjectID ( )
273- let context = objectReceived [ "@context" ] ? { "@context" : objectReceived [ "@context" ] } : { }
274269 let rerumProp = { "__rerum" : utils . configureRerumOptions ( generatorAgent , originalObject , true , false ) [ "__rerum" ] }
275270 delete objectReceived [ "_rerum" ]
276271 delete objectReceived [ "_id" ]
277272 delete objectReceived [ "@id" ]
273+ if ( _contextid ( objectReceived [ "@context" ] ) ) {
274+ // id is also protected in this case, so it can't be set.
275+ delete objectReceived . id
276+ }
278277 delete objectReceived [ "@context" ]
278+
279279 let newObject = Object . assign ( context , { "@id" : process . env . RERUM_ID_PREFIX + id } , objectReceived , rerumProp , { "_id" : id } )
280280 console . log ( "UPDATE" )
281281 try {
@@ -285,7 +285,7 @@ const putUpdate = async function (req, res, next) {
285285 res . set ( utils . configureWebAnnoHeadersFor ( newObject ) )
286286 res . location ( newObject [ "@id" ] )
287287 res . status ( 200 )
288- delete newObject . _id
288+ newObject = idNegotiation ( newObject )
289289 newObject . new_obj_state = JSON . parse ( JSON . stringify ( newObject ) )
290290 res . json ( newObject )
291291 return
@@ -331,16 +331,19 @@ async function _import(req, res, next) {
331331 delete objectReceived [ "_rerum" ]
332332 delete objectReceived [ "_id" ]
333333 delete objectReceived [ "@id" ]
334- delete objectReceived [ "id" ]
335334 delete objectReceived [ "@context" ]
335+ if ( _contextid ( objectReceived [ "@context" ] ) ) {
336+ // id is also protected in this case, so it can't be set.
337+ delete objectReceived . id
338+ }
336339 let newObject = Object . assign ( context , { "@id" : process . env . RERUM_ID_PREFIX + id } , objectReceived , rerumProp , { "_id" : id } )
337340 console . log ( "IMPORT" )
338341 try {
339342 let result = await db . insertOne ( newObject )
340343 res . set ( utils . configureWebAnnoHeadersFor ( newObject ) )
341344 res . location ( newObject [ "@id" ] )
342345 res . status ( 200 )
343- delete newObject . _id
346+ newObject = idNegotiation ( newObject )
344347 newObject . new_obj_state = JSON . parse ( JSON . stringify ( newObject ) )
345348 res . json ( newObject )
346349 }
@@ -392,6 +395,10 @@ const patchUpdate = async function (req, res, next) {
392395 delete objectReceived . __rerum //can't patch this
393396 delete objectReceived . _id //can't patch this
394397 delete objectReceived [ "@id" ] //can't patch this
398+ if ( _contextid ( objectReceived [ "@context" ] ) ) {
399+ // id is also protected in this case, so it can't be set.
400+ delete objectReceived . id
401+ }
395402 //A patch only alters existing keys. Remove non-existent keys from the object received in the request body.
396403 for ( let k in objectReceived ) {
397404 if ( originalObject . hasOwnProperty ( k ) ) {
@@ -413,7 +420,7 @@ const patchUpdate = async function (req, res, next) {
413420 res . set ( utils . configureWebAnnoHeadersFor ( originalObject ) )
414421 res . location ( originalObject [ "@id" ] )
415422 res . status ( 200 )
416- delete originalObject . _id
423+ originalObject = idNegotiation ( originalObject )
417424 originalObject . new_obj_state = JSON . parse ( JSON . stringify ( originalObject ) )
418425 res . json ( originalObject )
419426 return
@@ -424,6 +431,10 @@ const patchUpdate = async function (req, res, next) {
424431 delete patchedObject [ "_rerum" ]
425432 delete patchedObject [ "_id" ]
426433 delete patchedObject [ "@id" ]
434+ if ( _contextid ( patchedObject [ "@context" ] ) ) {
435+ // id is also protected in this case, so it can't be set.
436+ delete patchedObject . id
437+ }
427438 delete patchedObject [ "@context" ]
428439 let newObject = Object . assign ( context , { "@id" : process . env . RERUM_ID_PREFIX + id } , patchedObject , rerumProp , { "_id" : id } )
429440 console . log ( "PATCH UPDATE" )
@@ -434,7 +445,7 @@ const patchUpdate = async function (req, res, next) {
434445 res . set ( utils . configureWebAnnoHeadersFor ( newObject ) )
435446 res . location ( newObject [ "@id" ] )
436447 res . status ( 200 )
437- delete newObject . _id
448+ newObject = idNegotiation ( newObject )
438449 newObject . new_obj_state = JSON . parse ( JSON . stringify ( newObject ) )
439450 res . json ( newObject )
440451 return
@@ -473,6 +484,7 @@ const patchSet = async function (req, res, next) {
473484 let err = { message : `` }
474485 res . set ( "Content-Type" , "application/json; charset=utf-8" )
475486 let objectReceived = JSON . parse ( JSON . stringify ( req . body ) )
487+ let originalContext
476488 let patchedObject = { }
477489 let generatorAgent = getAgentClaim ( req , next )
478490 if ( objectReceived [ "@id" ] ) {
@@ -500,6 +512,10 @@ const patchSet = async function (req, res, next) {
500512 }
501513 else {
502514 patchedObject = JSON . parse ( JSON . stringify ( originalObject ) )
515+ if ( _contextid ( patchedObject [ "@context" ] ) ) {
516+ // id is also protected in this case, so it can't be set.
517+ delete patchedObject . id
518+ }
503519 //A set only adds new keys. If the original object had the key, it is ignored here.
504520 for ( let k in objectReceived ) {
505521 if ( originalObject . hasOwnProperty ( k ) ) {
@@ -516,7 +532,7 @@ const patchSet = async function (req, res, next) {
516532 res . set ( utils . configureWebAnnoHeadersFor ( originalObject ) )
517533 res . location ( originalObject [ "@id" ] )
518534 res . status ( 200 )
519- delete originalObject . _id
535+ originalObject = idNegotiation ( originalObject )
520536 originalObject . new_obj_state = JSON . parse ( JSON . stringify ( originalObject ) )
521537 res . json ( originalObject )
522538 return
@@ -527,6 +543,10 @@ const patchSet = async function (req, res, next) {
527543 delete patchedObject [ "_rerum" ]
528544 delete patchedObject [ "_id" ]
529545 delete patchedObject [ "@id" ]
546+ if ( _contextid ( patchedObject [ "@context" ] ) ) {
547+ // id is also protected in this case, so it can't be set.
548+ delete patchedObject . id
549+ }
530550 delete patchedObject [ "@context" ]
531551 let newObject = Object . assign ( context , { "@id" : process . env . RERUM_ID_PREFIX + id } , patchedObject , rerumProp , { "_id" : id } )
532552 try {
@@ -536,7 +556,7 @@ const patchSet = async function (req, res, next) {
536556 res . set ( utils . configureWebAnnoHeadersFor ( newObject ) )
537557 res . location ( newObject [ "@id" ] )
538558 res . status ( 200 )
539- delete newObject . _id
559+ newObject = idNegotiation ( newObject )
540560 newObject . new_obj_state = JSON . parse ( JSON . stringify ( newObject ) )
541561 res . json ( newObject )
542562 return
@@ -605,6 +625,10 @@ const patchUnset = async function (req, res, next) {
605625 delete objectReceived . _id //can't unset this
606626 delete objectReceived . __rerum //can't unset this
607627 delete objectReceived [ "@id" ] //can't unset this
628+ if ( _contextid ( objectReceived [ "@context" ] ) ) {
629+ // id is also protected in this case, so it can't be set.
630+ delete objectReceived . id
631+ }
608632 /**
609633 * unset does not alter an existing key. It removes an existing key.
610634 * The request payload had {key:null} to flag keys to be removed.
@@ -625,7 +649,7 @@ const patchUnset = async function (req, res, next) {
625649 res . set ( utils . configureWebAnnoHeadersFor ( originalObject ) )
626650 res . location ( originalObject [ "@id" ] )
627651 res . status ( 200 )
628- delete originalObject . _id
652+ originalObject = idNegotiation ( originalObject )
629653 originalObject . new_obj_state = JSON . parse ( JSON . stringify ( originalObject ) )
630654 res . json ( originalObject )
631655 return
@@ -636,7 +660,12 @@ const patchUnset = async function (req, res, next) {
636660 delete patchedObject [ "_rerum" ]
637661 delete patchedObject [ "_id" ]
638662 delete patchedObject [ "@id" ]
663+ if ( _contextid ( patchedObject [ "@context" ] ) ) {
664+ // id is also protected in this case, so it can't be set.
665+ delete patchedObject . id
666+ }
639667 delete patchedObject [ "@context" ]
668+
640669 let newObject = Object . assign ( context , { "@id" : process . env . RERUM_ID_PREFIX + id } , patchedObject , rerumProp , { "_id" : id } )
641670 console . log ( "PATCH UNSET" )
642671 try {
@@ -646,7 +675,7 @@ const patchUnset = async function (req, res, next) {
646675 res . set ( utils . configureWebAnnoHeadersFor ( newObject ) )
647676 res . location ( newObject [ "@id" ] )
648677 res . status ( 200 )
649- delete newObject . _id
678+ newObject = idNegotiation ( newObject )
650679 newObject . new_obj_state = JSON . parse ( JSON . stringify ( newObject ) )
651680 res . json ( newObject )
652681 return
@@ -728,6 +757,10 @@ const overwrite = async function (req, res, next) {
728757 delete objectReceived [ "@context" ]
729758 delete objectReceived [ "_id" ]
730759 delete objectReceived [ "__rerum" ]
760+ if ( _contextid ( objectReceived [ "@context" ] ) ) {
761+ // id is also protected in this case, so it can't be set.
762+ delete objectReceived . id
763+ }
731764 let newObject = Object . assign ( context , { "@id" : originalObject [ "@id" ] } , objectReceived , rerumProp , { "_id" : id } )
732765 let result
733766 try {
@@ -740,7 +773,7 @@ const overwrite = async function (req, res, next) {
740773 }
741774 res . set ( utils . configureWebAnnoHeadersFor ( newObject ) )
742775 res . location ( newObject [ "@id" ] )
743- delete newObject . _id
776+ newObject = idNegotiation ( newObject )
744777 newObject . new_obj_state = JSON . parse ( JSON . stringify ( newObject ) )
745778 res . json ( newObject )
746779 return
@@ -856,7 +889,7 @@ const release = async function (req, res, next) {
856889 res . set ( utils . configureWebAnnoHeadersFor ( releasedObject ) )
857890 res . location ( releasedObject [ "@id" ] )
858891 console . log ( releasedObject . _id + " has been released" )
859- delete releasedObject . _id
892+ releasedObject = idNegotiation ( releasedObject )
860893 releasedObject . new_obj_state = JSON . parse ( JSON . stringify ( releasedObject ) )
861894 res . json ( releasedObject )
862895 return
@@ -895,11 +928,7 @@ const query = async function (req, res, next) {
895928 }
896929 try {
897930 let matches = await db . find ( props ) . limit ( limit ) . skip ( skip ) . toArray ( )
898- matches =
899- matches . map ( o => {
900- delete o . _id
901- return o
902- } )
931+ matches = matches . map ( o => idNegotiation ( o ) )
903932 res . set ( utils . configureLDHeadersFor ( matches ) )
904933 res . json ( matches )
905934 } catch ( error ) {
@@ -924,7 +953,7 @@ const id = async function (req, res, next) {
924953 //Support requests with 'If-Modified_Since' headers
925954 res . set ( utils . configureLastModifiedHeader ( match ) )
926955 res . location ( match [ "@id" ] )
927- delete match . _id
956+ match = idNegotiation ( match )
928957 res . json ( match )
929958 return
930959 }
@@ -975,10 +1004,14 @@ const bulkCreate = async function (req, res, next) {
9751004 // }
9761005 let bulkOps = [ ]
9771006 documents . forEach ( d => {
978- const id = ObjectID ( )
1007+ const providedID = d . _id
1008+ const id = isValidID ( providedID ) ? providedID : ObjectID ( )
9791009 let generatorAgent = getAgentClaim ( req , next )
9801010 d = utils . configureRerumOptions ( generatorAgent , d )
981- // TODO: check profiles/parameters for 'id' vs '@id' and use that
1011+ if ( _contextid ( provided [ "@context" ] ) ) {
1012+ // id is also protected in this case, so it can't be set.
1013+ delete d . id
1014+ }
9821015 d . _id = id
9831016 d [ '@id' ] = `${ process . env . RERUM_ID_PREFIX } ${ id } `
9841017 bulkOps . push ( { insertOne : { "document" : d } } )
@@ -990,7 +1023,7 @@ const bulkCreate = async function (req, res, next) {
9901023 res . status ( 201 )
9911024 const estimatedResults = bulkOps . map ( f => {
9921025 let doc = f . insertOne . document
993- delete doc . _id
1026+ doc = idNegotiation ( doc )
9941027 return doc
9951028 } )
9961029 res . json ( estimatedResults ) // https://www.rfc-editor.org/rfc/rfc7231#section-6.3.2
@@ -1079,10 +1112,7 @@ const since = async function (req, res, next) {
10791112 } )
10801113 let descendants = getAllDescendants ( all , obj , [ ] )
10811114 descendants =
1082- descendants . map ( o => {
1083- delete o . _id
1084- return o
1085- } )
1115+ descendants . map ( o => idNegotiation ( o ) )
10861116 res . set ( utils . configureLDHeadersFor ( descendants ) )
10871117 res . json ( descendants )
10881118}
@@ -1120,10 +1150,7 @@ const history = async function (req, res, next) {
11201150 } )
11211151 let ancestors = getAllAncestors ( all , obj , [ ] )
11221152 ancestors =
1123- ancestors . map ( o => {
1124- delete o . _id
1125- return o
1126- } )
1153+ ancestors . map ( o => idNegotiation ( o ) )
11271154 res . set ( utils . configureLDHeadersFor ( ancestors ) )
11281155 res . json ( ancestors )
11291156}
0 commit comments