C. Alexis
C. Alexis

Reputation: 11

Java Springboot metadata: Reference map key dynamically as value of another property for hints

All of this was tested/tried on springboot 3.3.0.

Consider the following configuration class file:

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Map;

@ConfigurationProperties(prefix = "app")
public record AppProperties(Map<String, MapConfig> map, UseConfig config) {

    record MapConfig(String value1, String value2) {}
    record UseConfig(String use) {}
}

And now consider the following configuration yaml file:

app:
  map: <-- This is a Map<String, Object>
    key1: <-- This is a user defined key that I cannot control through enums, so it's a String
      ...
    key2:
      ...
  config:
    use: key1 <-- Use one key from the `app.map` keySet

What I want to do, is to provide a nice user experience by having hints provided by the IDE.

  1. I looked into how map are being tested as part of the spring-boot-configuration-metadata project.
  2. I looked into the Metadata Format :: Spring Boot docs

But it seems that I cannot provide dynamic hints based on the keys of a map.

I tried configuring hints manually, but couldn't find a way with the current value providers:

I would have expected something like that to fulfill my needs:


{
  "name": "app.config.use",
  "providers": [
    {
      "name": "property-source",
      "parameters": {
        "property": "app.map.keys"
      }
    }
  ]
}

Note that I'm using .keys to specify the key set of such property. Just as mentioned in the value hint doc which states:

If your property is of type Map, you can provide hints for both the keys and the values (but not for the map itself). The special .keys and .values suffixes must refer to the keys and the values, respectively.

Upvotes: 1

Views: 59

Answers (0)

Related Questions