DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • How To Approach Java, Databases, and SQL [Video]
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers

Trending

  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • How To Approach Java, Databases, and SQL [Video]
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Content Filter Pattern for REST Service Fields Filtering

Content Filter Pattern for REST Service Fields Filtering

What if the service consumer doesn’t want all the fields all the time? They want to have the response filtered. Content Filter to the rescue!

Patryk Bandurski user avatar by
Patryk Bandurski
·
Jul. 13, 20 · Analysis
Like (3)
Save
Tweet
Share
7.21K Views

Join the DZone community and get the full member experience.

Join For Free

At some point, you may have a service that returns a lot of fields and related objects. What if the service consumer doesn’t want all the fields all the time? In other words, he/she would like to have the response filtered. Here comes the Content Filter to the rescue. After you read this article you will find some ideas about how to use it in your own scenarios. So let’s roll. 

Content Filter

This is the opposite pattern to Enrchier Pattern – more about it you can find here. In this particular scenario, we want to simplify our response. Remove unnecessary information that the receiver doesn’t need.

Content Filter pattern

As you can see in the diagram above, before filter we have a message with 3 elements and after the filter is applied we have only one element. The magic is happening in the black boxes called Content Filter. However, we as designers should know how this particular element works. So, how to define filter?

We may choose two ways of doing this:

  • Positive filter – we specify what we would like to have in the response
  • Negative filter – we specify what should be removed from the original payload

Filtering in Mule 4 Using DataWeave 2.2

In order to filter out some fields, we have DataWeave language. So for filtering purposes, we will use the Transform Message component. We can filter properties from objects and arrays. So let see how to do the positive and negative filtering in DW.

Negative Filtering

In DataWeave we have two operators – and –. The first one allows for removing a key-value pair by providing the key.

title property

Remove the title property

Okay, that way I can remove one field at a time. In order to remove two fields like in the example above I would need to do it in the following way:

JavaScript
 




x


 
1
%dw 2.0
2
output application/json
3
---
4
{
5
    "id": "idy4234-2",
6
    "title": "Meetup"
7
} - "id" - "title"



The double dash operator (–) allows providing keys array. In other words, each key present in the array will be removed from the supplied object. 

title and id

Remove both title and id properties

In the example above we have provided an array with title and id keys. As a result of applying — operator, we got the empty object.

Positive Filtering

This is the opposite scenario in contrast to previously described negative filtering. This time we would like to define which properties should stay, not mentioned ones should be removed from the final outcome.

filterObject

filterObject function to filter by custom condition

We don’t have a similar operator to perform that action. filterObject function is the best match for that case. This function iterates over each key and applies lambda expression. If the condition is evaluated to true, include the field in the output. Otherwise, omit it.

Below you can see a code snippet showing how to do a positive filter. We instruct filterObject to include the key-value pair only if the key is present in the array – here “id”.

Java
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4
{
5
    "id": "idy4234-2",
6
    "title": "Meetup"
7
} filterObject ((value, key) -> ["id"] contains key as String)



Filter Fields in Your REST Service

I have looked for may publicly available services to see some good guidelines to do the filter. However, a lot of them are too complex or simple. Here what you can see, is some version in between. Below you can see a trait filter definition.

anypoint exchange

filter query parameter description on Anypoint Exchange

As you can see I have decided to introduce the default value *all indicating that all fields will be returned. To distinguish negative and positive filtering I have decided to introduce – (minus) prefix before fields to remove from the final response. Values are separated by the comma without any additional space.

Warning: We can’t mix the positive and negative filters. I have decided on this rule because it makes sense. Either we want to specify what should be returned or skipped.

Here is the link to the trait filterable. You can place it some common traits library or in specif API directly. 

Use Trait in Your API Specification

In order to use an externally defined trait, you need to include it in the API specification. You will do this like in the snippet below:

Java
 




xxxxxxxxxx
1
13


 
1
#%RAML 1.0
2
title: Consent Experience API
3
version: v1
4
 
          
5
baseUri: https://sfdc-consents.profit-online.pl/api/{version}/
6
 
          
7
mediaType: [ application/json ]
8
 
          
9
types:
10
  consent: !include types/consent.raml
11
 
          
12
traits:
13
  filterable: !include traits/filterable.raml



As you can see declared filterable trait in traits section. How to apply this to the resource?

You need to specify the array of traits using is, like in the second line on the snippet below.

Java
 




xxxxxxxxxx
1


 
1
/consents:
2
  is: [filterable]
3
  
4
  get:
5
    responses:
6
      200:
7
        body: consent[]



Example Usage

The first case is the call to retrieve all the fields

Java
 




xxxxxxxxxx
1


 
1
GET /mongodb/sapi/consents HTTP/1.1
2
Host: ambassadorpatryk.com



or

Java
 




xxxxxxxxxx
1


 
1
GET /mongodb/sapi/consents?filter=*all HTTP/1.1
2
Host: ambassadorpatryk.com



The second case is to retrieve only specified fields:

Java
 




xxxxxxxxxx
1


 
1
GET /mongodb/sapi/consents?filter=id,name,title HTTP/1.1
2
Host: ambassadorpatryk.com



The third case is to retrieve the response without the specified fields:

Java
 




xxxxxxxxxx
1


 
1
GET /mongodb/sapi/consents?filter=-details,-id HTTP/1.1
2
Host: ambassadorpatryk.com



Summary

I think that the presented solution for filtering fields gives some flexibility but isn’t overcomplicated. In many publicly, available APIs providers give the possibility to decide which fields are expected at the response. In the next article, I will introduce my filter DataWeave module. Stay tuned!

If you have any other ideas on how to introduce filtering fill free to share it. Let’s learn from each other.

Have a great day!

More my content you can find on my personal blog, dedicated to MuleSoft Integration.

Filter (software) REST Web Protocols Trait (computer programming)

Published at DZone with permission of Patryk Bandurski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • How To Approach Java, Databases, and SQL [Video]
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: