-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for populating Meta object in JSON-API (#2824)
* Adding support for populating Meta object in JSON-API * Added WithMeatadata interface * Fixed checkstyle * Removed unnecessary interface prototypes in RequestScope * Added unit test. * Added IT test for in memory store for metadata JSON-API * Added IT test. * Fixed checkstyles and NPE * Moved logic to add resource meta to persistentResource.toMeta * Fixed checkstyles and added one more UT * Another checkstyle error * Inspection rework
- Loading branch information
Showing
18 changed files
with
464 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...core/src/main/java/com/yahoo/elide/jsonapi/document/processors/PopulateMetaProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2022, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
|
||
package com.yahoo.elide.jsonapi.document.processors; | ||
|
||
import com.yahoo.elide.core.PersistentResource; | ||
import com.yahoo.elide.core.RequestScope; | ||
import com.yahoo.elide.jsonapi.models.JsonApiDocument; | ||
import com.yahoo.elide.jsonapi.models.Meta; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
/** | ||
* Document processor that populates 'meta' fields for collections (from request scope metadata) and | ||
* resources (from resource metadata). This processor runs after the document has already been populated. | ||
*/ | ||
public class PopulateMetaProcessor implements DocumentProcessor { | ||
@Override | ||
public void execute( | ||
JsonApiDocument jsonApiDocument, | ||
RequestScope scope, | ||
PersistentResource persistentResource, | ||
MultivaluedMap<String, String> queryParams | ||
) { | ||
|
||
addDocumentMeta(jsonApiDocument, scope); | ||
} | ||
|
||
private void addDocumentMeta(JsonApiDocument document, RequestScope scope) { | ||
Set<String> fields = scope.getMetadataFields(); | ||
if (fields.size() == 0) { | ||
return; | ||
} | ||
Meta meta = document.getMeta(); | ||
if (meta == null) { | ||
meta = new Meta(new HashMap<>()); | ||
} | ||
|
||
for (String field : fields) { | ||
meta.getMetaMap().put(field, scope.getMetadataField(field).get()); | ||
} | ||
|
||
document.setMeta(meta); | ||
} | ||
|
||
@Override | ||
public void execute( | ||
JsonApiDocument jsonApiDocument, | ||
RequestScope scope, | ||
LinkedHashSet<PersistentResource> resources, | ||
MultivaluedMap<String, String> queryParams | ||
) { | ||
addDocumentMeta(jsonApiDocument, scope); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
elide-core/src/main/java/com/yahoo/elide/jsonapi/document/processors/WithMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2022, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
|
||
package com.yahoo.elide.jsonapi.document.processors; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* The class carries metadata fields. | ||
*/ | ||
public interface WithMetadata { | ||
|
||
/** | ||
* Sets a metadata property for this request. | ||
* @param property | ||
* @param value | ||
*/ | ||
default void setMetadataField(String property, Object value) { | ||
//noop | ||
} | ||
|
||
/** | ||
* Retrieves a metadata property from this request. | ||
* @param property | ||
* @return An optional metadata property. | ||
*/ | ||
Optional<Object> getMetadataField(String property); | ||
|
||
/** | ||
* Return the set of metadata fields that have been set. | ||
* @return metadata fields that have been set. | ||
*/ | ||
Set<String> getMetadataFields(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.