Skip to content

Conversation

@thomasdarimont
Copy link
Contributor

@thomasdarimont thomasdarimont commented Jul 13, 2025

PoC for a field selection / projection mechanism

The projection works on the Jackson ObjectMapper level and uses a SimpleBeanPropertyFilter (FieldProjectionFilter) to control the properties during JSON marshalling. Consumers can select the desired result fields with a fields query parameter. Note that the selection only works with top-level object properties. There is currently no support for nested property selection.

Currently, all representations inheriting from BaseRepresentation support this filtering via the JsonFilter annotation.

JAX-RS resource methods which declare a
@QueryParam(Projections.PARAM) Projections projections become eligible for projections.

The explicit parameter declaration ensures that the projection parameter can be inferred for the OpenAPI metadata.

A request without the fields parameter will return the full resource:

curl -v -H "Authorization: Bearer $KC_ACCESS_TOKEN" \
http://localhost:8081/auth/admin/api/v2/realms/adminapiv2/clients/custom-client1 | jq -C

A request with the included filter:

curl -v -H "Authorization: Bearer $KC_ACCESS_TOKEN" \
http://localhost:8081/auth/admin/api/v2/realms/adminapiv2/clients/custom-client1?fields=clientId,displayName

will only return the selected fields.

curl -s -H "Authorization: Bearer $KC_ACCESS_TOKEN" \
http://localhost:8081/auth/admin/api/v2/realms/adminapiv2/clients?fields=clientId

Yields:

[
  {
    "clientId": "account"
  },
  {
    "clientId": "account-console"
  },
  {
    "clientId": "admin-api"
  },
  {
    "clientId": "admin-cli"
  },
  {
    "clientId": "broker"
  },
  {
    "clientId": "custom-client1"
  },
  {
    "clientId": "realm-management"
  },
  {
    "clientId": "security-admin-console"
  }
]

@thomasdarimont thomasdarimont changed the title [Admin API v2] Skeleton prototype (#39322) [Admin API v2] PoC for a field selection / projection mechanism Jul 13, 2025
@thomasdarimont
Copy link
Contributor Author

Currently I use the query parameter fields to control the projections. However some representations might use the property fields it might be better to use a different name or prefix differentiate "technical" parameters from actual resource properties.

@thomasdarimont thomasdarimont changed the title [Admin API v2] PoC for a field selection / projection mechanism [Admin API v2] PoC for a field projection mechanism Jul 13, 2025
@vmuzikar vmuzikar added the team/admin-api-wg Admin API v2 Working Group label Jul 15, 2025
@mabartos mabartos force-pushed the feature/admin-api-v2 branch from 562be58 to aadbf3f Compare July 22, 2025 11:19
@vmuzikar
Copy link
Contributor

@thomasdarimont Can you please rebase?

Copy link
Contributor

@vmuzikar vmuzikar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall IMHO this approach makes sense.

Just to note, this purely benefits the network/client side, not the server side as we're still fetching all fields (which might underneath do some unnecessary JOINs, etc.). But wa could quite easily detect the fields in the query param and reflect them when fetching from the model, if needed.

@thomasdarimont thomasdarimont force-pushed the feature/admin-api-v2-projections branch from 3266164 to 057eeb4 Compare July 24, 2025 20:05
@thomasdarimont
Copy link
Contributor Author

I rebased the branch as discussed.

@mabartos mabartos force-pushed the feature/admin-api-v2 branch from 266bb90 to d207105 Compare August 5, 2025 14:49
@mabartos
Copy link
Contributor

mabartos commented Aug 8, 2025

@thomasdarimont Sorry, another rebase is needed.

Copy link
Contributor

@mabartos mabartos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomasdarimont Looks good to me from the first sight.

Just a consideration:

  • We could even support to exclude some fields as providing fields=!clientId,!auth would return the whole representation without the clientId and auth fields.

WDYT?

@mabartos mabartos force-pushed the feature/admin-api-v2 branch from 02245d9 to 17e8407 Compare September 5, 2025 09:08
The projection works on the Jackson ObjectMapper level and uses a SimpleBeanPropertyFilter (FieldProjectionFilter) to control the properties during JSON marshalling. Consumers can select the desired result fields with a `fields` query parameter. Note that the selection only works with top-level object properties. There is currently no support for nested property selection.

Currently, all representations inheriting from BaseRepresentation support this filtering via the JsonFilter annotation.

JAX-RS resource methods which declare a
`@QueryParam(Projections.PARAM) Projections projections` become eligible for projections.

The explicit parameter declaration ensures that the projection parameter can be inferred for the OpenAPI metadata.

A request without the fields parameter will return the full resource:

```
curl -v -H "Authorization: Bearer $KC_ACCESS_TOKEN" \
http://localhost:8081/auth/admin/api/v2/realms/adminapiv2/clients/custom-client1 | jq -C
```

A request with the included filter:
```
curl -v -H "Authorization: Bearer $KC_ACCESS_TOKEN" \
http://localhost:8081/auth/admin/api/v2/realms/adminapiv2/clients/custom-client1?fields=clientId,displayName
```
Will only return the selected fields.

Signed-off-by: Thomas Darimont <[email protected]>
(cherry picked from commit 3266164)
(cherry picked from commit 057eeb4)
@thomasdarimont thomasdarimont force-pushed the feature/admin-api-v2-projections branch from 057eeb4 to 3aa72d8 Compare September 15, 2025 09:36
@thomasdarimont
Copy link
Contributor Author

thomasdarimont commented Sep 15, 2025

We could even support to exclude some fields as providing fields=!clientId,!auth would return the whole representation without the clientId and auth fields.

Specifying exclusions via ?fields=!field1,!field2 can work, however if you have explicit projection ?fields=field1,field2 and explicit omission ?fields=!fieldA,!fieldB how should those work together? I think we'd need a special symbol to express that we want to select all fields, exception the ones explicitly omitted. Perhaps something like ?fields=ALL,!fieldA,!fieldB might work.

@sschu
Copy link
Contributor

sschu commented Sep 15, 2025

@thomasdarimont I would say it is either all included fields or all excluded fields but not a combination of both.

@sschu
Copy link
Contributor

sschu commented Sep 24, 2025

I am wondering if the projection mechanism actually means all fields become optional in the OpenAPI spec (at least when reading from the server).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team/admin-api-wg Admin API v2 Working Group

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants