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

Invalid param "not an object" when using {{#tests}} on a non-object #37

Closed
Niols opened this issue Dec 11, 2018 · 5 comments
Closed

Invalid param "not an object" when using {{#tests}} on a non-object #37

Niols opened this issue Dec 11, 2018 · 5 comments

Comments

@Niols
Copy link

Niols commented Dec 11, 2018

Hello,

I was trying to use Mustache to render a string only when it is non-null. This should essentially be the following template:

Hello
{{#key}}- The key exists: {{key}}{{/key}}
Good bye

It works as expected with the Ruby implementation of Mustache in both cases. It does not however work with the OCaml implementation in the case where key exists. I get Mustache_types.Invalid_param("str. not an object").

My guess is that Mustache is happy with the test {{#key}} and puts the JSON for key in the context. When later rendering {{key}} it fails because the context is not an object. It's actually a bit more than a guess:

In render_fmt, when rendering sections, we have:

| context -> render' indent s.contents (add_context context js)

where add_context is:

let add_context ctx js =
match (ctx, js) with
| `O ctx_o, `O js_o -> `O (ctx_o @ js_o)
| _, _ -> ctx
in

which means that when ctx is not an object, the context becomes this non-object. And when we try to render something afterwards, we have a failure. I would rather have something like:

    let add_context ctx js =
      match (ctx, js) with
      | `O ctx_o, `O js_o -> `O (ctx_o @ js_o)
      | _, `O _ -> js
      | _, _ -> ctx
    in

Does that seem reasonable to you?

Niols added a commit to Niols/ocaml-mustache that referenced this issue Dec 14, 2018
@rgrinberg
Copy link
Owner

Yeah, that seems reasonable to me.

@tsileo
Copy link

tsileo commented May 8, 2019

Hey, is there any way to have the fix in a new opam release?

Should I make a PR (with the fix from @Niols fork)?

Thanks!

@rgrinberg
Copy link
Owner

Yes, that would be welcome.

tsileo added a commit to tsileo/ocaml-mustache that referenced this issue May 8, 2019
@gasche
Copy link
Collaborator

gasche commented Dec 25, 2020

I hit this issue independently and proposed a fix in #49. My proposal is more invasive, but it more clearly corresponds to the specification, and there is no regression in existing tests.

@gasche
Copy link
Collaborator

gasche commented Dec 25, 2020

(The fix in #49 has been merged, so I think this issue and the other PR #41 could be closed.)

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

4 participants