gestalt-config / gestalt

A Java configuration library
https://gestalt-config.github.io/gestalt/
Apache License 2.0
81 stars 2 forks source link

Allow empty arrays in proxy #127

Closed acavalli-lunalabs closed 11 months ago

acavalli-lunalabs commented 11 months ago

I have the following hocon config file:

database: {
  global: {
    volumes: []
  }
}

If the volumes array is empty, i get the following error when I try to read globalCfg.getVolumes(); :

org.github.gestalt.config.exceptions.GestaltException: Failed to get proxy config while calling method: volumes in path: database.global.

I noticed that the class ProxyInvocationHandler inside ProxyDecoder.java does not have any value related to null properties and empty array properties.

credmond-git commented 11 months ago

Thanks for the bug report, generally gestalt fails if there is no data. But i will try and make it a little more flexible to meet your needs.

cavallium commented 11 months ago

Yes, but in some formats, like hocon, an array with 0 elements is different than "null", it's impossible to get that property even if it's valid

credmond-git commented 11 months ago

Gestalt supports loading from 7 different formats, and has the capability to merge them. So we load each format, but then convert it into an internal representation. Unfortunately, From there we lose some of the special properties of each format.

I have added a new configuration flag treatEmptyCollectionAsErrors you can set on the builder. Please set this to false, to get your desired behavior. By default Gestalt will treat empty (null, size 0) collections as an error. By setting this to false, if there is an empty collection it will simply return an empty collection.

Hopefully this will resolve the issue. But i haven't tested specifically with a proxy object, but only with a list, set and array.

Once this is released please let me know if this works for you.

cavallium commented 11 months ago

It doesn't work on empty arrays. Attempting to get the empty array causes the following error:

org.github.gestalt.config.exceptions.GestaltException: Failed getting config path: database, for class: it.cavallium.rockserver.core.config.DatabaseConfig
 - level: MISSING_VALUE, message: Array on path: database.global.volumes, has no value attempting to decode Array
 - level: ERROR, message: Decoding object : GlobalDatabaseConfig on path: database.global.volumes, field volumes results in null value

The config looks like this:

database: {
  global: {
    volumes: []
  }
}
cavallium commented 11 months ago

If I set .setTreatNullValuesInClassAsErrors(false), it still doesn't help:

org.github.gestalt.config.exceptions.GestaltException: Failed to get cached object from proxy config while calling method: volumes with type: class [Lit.cavallium.rockserver.core.config.VolumeConfig; in path: database.global.
    at org.github.gestalt.core/org.github.gestalt.config.decoder.ProxyDecoder$ProxyCacheInvocationHandler.invoke(ProxyDecoder.java:194)
    at jdk.proxy2/jdk.proxy2.$Proxy8.volumes(Unknown Source)
credmond-git commented 11 months ago

Can you please try version 0.24.2 and let me know if it fixes the issue for you.

cavallium commented 11 months ago

It works, thanks!