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

Returning core context in responses if Accept: application/ld+json #1637

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
* Forbidden to DELETE the Core Context !!! (it can be reloaded)
* Bug fix: JSON NULL literal is no longer forwarded!
* Improved treatment of malfunctioning notification receivers, on timeout (for clients that don't respond to the notification)
* Core context added to response if @context in payload body (Accept: application/ld+json)

## New Features:
* Support for attributes of type VocabularyProperty
* Support for attributes of type JsonProperty
* Support for the new URL parameter "format" for output formats (normalized, concise, simplified)
* New service: DELETE /ngsi-ld/v1/entities (URL param 'type' only - the rest are missing still)
* Distributed Subscriptions
* New service: DELETE /ngsi-ld/v1/entities (URL param 'type' only - the rest of the options are still missing)
* First draft of Distributed Subscriptions

## Notes
* TRoE is still not prepared for attributes of type Vocab/Json/Language, so, attributes of those types are not stored in the historical database
Expand Down
80 changes: 80 additions & 0 deletions src/lib/orionld/mhd/mhdConnectionTreat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

extern "C"
{
#include "kbase/kMacros.h" // K_FT
#include "kbase/kTime.h" // kTimeGet
#include "kbase/kStringSplit.h" // kStringSplit
#include "kjson/KjNode.h" // KjNode
Expand Down Expand Up @@ -458,6 +459,76 @@ static bool linkGet(const char* link)



// -----------------------------------------------------------------------------
//
// coreContextToResponse -
//
static void coreContextToResponse(KjNode* responseP)
{
if (responseP->type == KjObject)
{
KjNode* contextP = kjLookup(responseP, "@context");

if (contextP != NULL)
{
if (contextP->type == KjString)
{
//
// If the string is the core context, all OK
// If not, we have to creat an array of the user context and the core context
//
if (strcmp(contextP->value.s, coreContextUrl) != 0)
{
KjNode* userContext = kjString(orionldState.kjsonP, NULL, contextP->value.s);
KjNode* coreContextNodeP = kjString(orionldState.kjsonP, NULL, coreContextUrl);

//
// Change contextP from String to Array
//
contextP->type = KjArray;
contextP->value.firstChildP = NULL;
contextP->lastChild = NULL;

kjChildAdd(contextP, userContext);
kjChildAdd(contextP, coreContextNodeP);
}
}
else if (contextP->type == KjObject)
{
KjNode* contextArray = kjArray(orionldState.kjsonP, "@context");
KjNode* coreContextNodeP = kjString(orionldState.kjsonP, NULL, coreContextUrl);

kjChildRemove(responseP, contextP);
kjChildAdd(contextArray, contextP);
kjChildAdd(contextArray, coreContextNodeP);
kjChildAdd(responseP, contextArray);
}
else if (contextP->type == KjArray)
{
// Last item must be the core context
KjNode* lastP = contextP->value.firstChildP;
while (lastP->next != NULL)
lastP = lastP->next;

if (strcmp(lastP->value.s, coreContextUrl) != 0)
{
KjNode* coreContextNodeP = kjString(orionldState.kjsonP, NULL, coreContextUrl);
kjChildAdd(contextP, coreContextNodeP);
}
}
}
}
else if (responseP->type == KjArray)
{
for (KjNode* entityP = responseP->value.firstChildP; entityP != NULL; entityP = entityP->next)
{
coreContextToResponse(entityP);
}
}
}



// -----------------------------------------------------------------------------
//
// contextToPayload -
Expand Down Expand Up @@ -527,6 +598,15 @@ static void contextToPayload(void)
{
// Any other type ??? Error
}

//
// Add the core context to the response?
//
if ((orionldState.serviceP->options & ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE) != 0)
{
if (orionldState.out.contentType == MT_JSONLD)
coreContextToResponse(orionldState.responseTree);
}
}


Expand Down
14 changes: 12 additions & 2 deletions src/lib/orionld/service/orionldServiceInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
}
else if (serviceP->serviceRoutine == orionldGetEntities)
{
serviceP->options |= ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE;

serviceP->uriParams |= ORIONLD_URIPARAM_OPTIONS;
serviceP->uriParams |= ORIONLD_URIPARAM_FORMAT;
serviceP->uriParams |= ORIONLD_URIPARAM_LIMIT;
Expand All @@ -245,6 +247,8 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
}
else if (serviceP->serviceRoutine == orionldGetEntity)
{
serviceP->options |= ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE;

serviceP->uriParams |= ORIONLD_URIPARAM_OPTIONS;
serviceP->uriParams |= ORIONLD_URIPARAM_FORMAT;
serviceP->uriParams |= ORIONLD_URIPARAM_ATTRS;
Expand Down Expand Up @@ -328,6 +332,8 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
}
else if (serviceP->serviceRoutine == orionldGetRegistrations)
{
serviceP->options |= ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE;

serviceP->uriParams |= ORIONLD_URIPARAM_OPTIONS;
serviceP->uriParams |= ORIONLD_URIPARAM_LIMIT;
serviceP->uriParams |= ORIONLD_URIPARAM_OFFSET;
Expand All @@ -349,6 +355,8 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
}
else if (serviceP->serviceRoutine == orionldGetRegistration)
{
serviceP->options |= ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE;

serviceP->uriParams |= ORIONLD_URIPARAM_OPTIONS;
}
else if (serviceP->serviceRoutine == orionldPatchRegistration)
Expand All @@ -368,7 +376,7 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
}
else if (serviceP->serviceRoutine == orionldGetSubscriptions)
{
serviceP->options |= ORIONLD_SERVICE_OPTION_DONT_ADD_CONTEXT_TO_RESPONSE_PAYLOAD;
serviceP->options |= ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE;

serviceP->uriParams |= ORIONLD_URIPARAM_OPTIONS;
serviceP->uriParams |= ORIONLD_URIPARAM_COUNT;
Expand All @@ -377,7 +385,7 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
}
else if (serviceP->serviceRoutine == orionldGetSubscription)
{
serviceP->options |= ORIONLD_SERVICE_OPTION_DONT_ADD_CONTEXT_TO_RESPONSE_PAYLOAD;
serviceP->options |= ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE;

serviceP->uriParams |= ORIONLD_URIPARAM_OPTIONS;
}
Expand Down Expand Up @@ -424,6 +432,8 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
{
serviceP->isBatchOp = true;

serviceP->options |= ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE;

serviceP->uriParams |= ORIONLD_URIPARAM_OPTIONS;
serviceP->uriParams |= ORIONLD_URIPARAM_COUNT;
serviceP->uriParams |= ORIONLD_URIPARAM_LIMIT;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/types/OrionLdRestService.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ typedef struct OrionLdRestServiceSimplifiedVector
#define ORIONLD_SERVICE_OPTION_EXPAND_ATTR (1 << 9)
#define ORIONLD_SERVICE_OPTION_ACCEPT_JSONLD_NULL (1 << 10)
#define ORIONLD_SERVICE_OPTION_DATASET_SUPPORT (1 << 11)

#define ORIONLD_SERVICE_OPTION_CORE_CONTEXT_IN_RESPONSE (1 << 12)


// -----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,15 @@ Date: REGEX(.*)
11. GET Entity from step 05 - see P100 as short-name (P100 present in test @context)
====================================================================================
HTTP/1.1 200 OK
Content-Length: 166
Content-Length: 235
Content-Type: application/ld+json
Date: REGEX(.*)

{
"@context": "https://fiware.github.io/NGSI-LD_TestSuite/ldContext/testContext.jsonld",
"@context": [
"https://fiware.github.io/NGSI-LD_TestSuite/ldContext/testContext.jsonld",
"https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.6.jsonld"
],
"P100": {
"type": "Property",
"value": 100
Expand Down
Loading
Loading