Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message lost if exceeding maxStringLength and emptyPropertyExclusionEnabled is true #55

Closed
waldeinburg opened this issue Mar 16, 2020 · 4 comments

Comments

@waldeinburg
Copy link

If maxStringLength is exceeded and emptyPropertyExclusionEnabled is true, Jackson UTF8JsonGenerator will throw a JsonGenerationException: "Can not write a string, expecting field name (context: Object)".

@vy
Copy link
Owner

vy commented Mar 16, 2020

There is something pretty weird going on when JsonGeneratorDelegate is wrapped with a TokenFilter where includeNull=false. I could not reproduce your issue with the following snippet:

@Test
public void test_maxStringLength_with_emptyPropertyExclusionEnabled() throws JsonProcessingException {

    // Create the event template.
    ObjectNode eventTemplateRootNode = JSON_NODE_FACTORY.objectNode();
    eventTemplateRootNode.put("message", "${json:message}");
    String eventTemplate = eventTemplateRootNode.toString();

    // Create the layout.
    int maxStringLength = 10;
    BuiltConfiguration configuration = ConfigurationBuilderFactory.newConfigurationBuilder().build();
    LogstashLayout layout = LogstashLayout
            .newBuilder()
            .setConfiguration(configuration)
            .setEventTemplate(eventTemplate)
            .setMaxStringLength(maxStringLength)
            .setEmptyPropertyExclusionEnabled(false)
            .build();

    // Create the log event.
    SimpleMessage message = new SimpleMessage(Strings.repeat('m', maxStringLength) + 'x');
    LogEvent logEvent = Log4jLogEvent
            .newBuilder()
            .setLoggerName(LogstashLayoutTest.class.getSimpleName())
            .setMessage(message)
            .build();

    // Check the serialized event.
    String serializedLogEvent = layout.toSerializable(logEvent);
    JsonNode rootNode = OBJECT_MAPPER.readTree(serializedLogEvent);
    assertThat(point(rootNode, "message").asText()).isEqualTo(message.getFormattedMessage());

}

Here layout.toSerializable(logEvent) doesn't thrown an exception, but the comparison still fails. I have filed FasterXML/jackson-core#609 for the issue.

@vy
Copy link
Owner

vy commented Mar 16, 2020

@waldeinburg, I have implemented a work around for FasterXML/jackson-core#609 in 634aec3. Would you mind checking if it works for you, please?

@waldeinburg
Copy link
Author

The workaround works, thanks!

@vy
Copy link
Owner

vy commented Mar 18, 2020

I've just released v1.0.2 including this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants