Skip to content

String % operand raises a coercion error instead of printf-style formatting #1340

Open

Description

@Nuhiat-Arefin

Jinja2 applies printf-style formatting to 'text' % operand. Jinjava parses % as the JUEL modulo operator and coerces the left operand to a number, so a string left operand fails to render.

Affected: master df50b0d7. Releases 2.7.6, 2.8.3 and 2.8.4 behave the same.

Reproduction

{{ 'number'%r }}

Jinja2 renders number. Jinjava raises:

com.hubspot.jinjava.interpret.FatalTemplateErrorsException: Cannot coerce 'number' of class java.lang.String to class java.lang.Double (incompatible value)

Root cause

ExtendedParser maps the % token to AstBinary.MOD (src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java:421-424). The JUEL implementation of MOD converts both operands to numbers, and there is no string formatting path for a string left operand.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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