Skip to content

Unexpected boolean value on deserialization #372

@Djaytan

Description

@Djaytan

Hello,

When I try to deserialize the following YAML file:

hostname: 'example.com'
port: 1234
isSslEnabled: true

My test fail because of this error message:

org.opentest4j.AssertionFailedError: 
Expecting value to be true but was false
Expected :true
Actual   :false

Whereas I can observe that in debug mode:

image

The unit test:

      // Given
      String yamlFileName = "whenDeserializing_withValidValues.yml";
      Path yamlFile =
          ResourcesHelper.getResourcePath(HostValidatingPropertiesTest.class, yamlFileName);

      // When
      Optional<HostValidatingProperties> optionalHostValidatingProperties =
          YamlDeserializer.deserialize(yamlFile, HostValidatingProperties.class);

      // Then
      assertThat(optionalHostValidatingProperties).isPresent();

      HostValidatingProperties hostValidatingProperties = optionalHostValidatingProperties.get();

      assertAll(() -> assertThat(hostValidatingProperties.getHostname()).isEqualTo("example.com"),
          () -> assertThat(hostValidatingProperties.getPort()).isEqualTo(1234),
          () -> assertThat(hostValidatingProperties.isSslEnabled()).isTrue());

And the YamlDeserializer class:

/**
 * YAML deserializer for testing purposes.
 */
public final class YamlDeserializer {

  private YamlDeserializer() {
    // Static class
  }

  /**
   * Deserializes the given YAML file into the specified type.
   *
   * @param yamlFile The YAML content to deserialize.
   * @param type The expected type of the YAML content.
   * @return The deserialized value if present and of valid type.
   * @param <T> The expected type to be obtained from deserialization.
   */
  @SneakyThrows
  public static <T> @NonNull Optional<T> deserialize(@NonNull Path yamlFile,
      @NonNull Class<T> type) {
    ConfigurationNode rootNode = buildYamlConfigurationNode(yamlFile);
    return Optional.ofNullable(rootNode.get(type));
  }

  @SneakyThrows
  private static @NonNull ConfigurationNode buildYamlConfigurationNode(@NonNull Path yamlFile) {
    return YamlConfigurationLoader.builder().path(yamlFile).build().load();
  }
}

Do you have any idea about why finally the deserialized boolean value has been converted to false? Is it a bug?

Thanks a lot for your time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions