YAMLMapper MismatchedInputException YAML key with periods

227 Views Asked by At

Error: com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input

Yaml file:

formatting.template:
  fields:
    - name: birthdate
      type: java.lang.String
      subType: java.util.Date
      lenght: 10

ConfigurationProperties:

@Data
public class FormattingConfigurationProperties {

    private List<Field> fields;

    @Data
    public static class Field {
        private String name;
        private String type;
        private String subType;
        private String lenght;
    }

}

Method to read yaml

private static FormattingConfigurationProperties buildFormattingConfigurationProperties() throws IOException {

    InputStream inputStream = new FileInputStream(new File("./src/test/resources/" + "application_formatting.yaml"));
    
    YAMLMapper mapper = new YAMLMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

    return mapper.readerFor(FormattingConfigurationProperties.class)
                 .at(/formatting/template)
                 .readValue(inputStream);

}

I actually solved it by changing the Yaml file, splitting formatting.template on separate lines:

formatting:
  template:
    fields:
      - name: birthdate
        type: java.lang.String
        subType: java.util.Date
        lenght: 10

This means that is not able to read key with dot (periods (.)). Someone know how to avoid MismatchedInputException, when the prefix is on the same line separated by dot?

1

There are 1 best solutions below

0
flyx On BEST ANSWER

You're using the JSON Pointer /formatting/template. This is for nested mappings, as shown in your second YAML file. If you have a condensed key formatting.template, you'll need the JSON Pointer /formatting.template instead.

YAML is perfectly able to read keys with a dot, it just does not do what you think it does. The dot is not a special character in YAML, just part of the content.

You may have worked with Spring which loads YAML files by rewriting them as Properties files, where . is a separator. Since the existing dots are not escaped, for YAML files used with Spring, a dot is the same as nested keys. However when you directly use a YAML loader, such as Jackson, that is not the case.