question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clarification of json 'data' section docs disparity

See original GitHub issue

Hello. First, thank you for this project, this is a great thing for FaaS at large 😃

In implementing the spec, there has been some confusion around 2 particular sections from the docs, I am hoping to have some light shed on which is the ‘right’ way and then to cement that in the documentation itself. I did not see any open issues or PRs at the moment that resolve this, but may have missed one.

Early in json-format.md section 2.3 https://github.com/cloudevents/spec/blob/master/json-format.md#23-mapping-object-typed-attributes notes (ATOW):

The CloudEvents data attribute is Object-typed, meaning that it either holds a String, or a Binary value, or a Map. Map entry values are also Object typed.

If an implementation determines that the actual type of an Object is a String, the value MUST be represented as JSON string expression; for Binary, the value MUST represented as JSON string expression containing the Base64 encoded binary value; for Map, the value MUST be represented as a JSON object expression, whereby the index fields become member names and the associated values become the respective member’s value.

where later on it goes on in 3.1 https://github.com/cloudevents/spec/blob/master/json-format.md#31-special-handling-of-the-data-attribute

The mapping of the Object-typed data attribute follows the rules laid out in Section 2.3., with one additional rule:

If an implementation determines that the type of the data attribute is Binary or String, it MUST inspect the contentType attribute to determine whether it is indicated that the data value contains JSON data.

If the contentType value is “application/json”, or any media type with a structured +json suffix, the implementation MUST translate the data attribute value into a JSON value, and set the data member of the envelope JSON object to this JSON value.

Unlike all other attributes, for which value types are restricted to strings per the type-system mapping, the resulting data member JSON value is unrestricted, and MAY also contain numeric and logical JSON types.

The disparity being that in 2.3 the spec seems to define that the “data” section can only contain a json string type (application/xml, binary, et al) or a json map type, where in 3.1 the type domain seems to broaden to any json value type (e.g. false, null, ["list"], map, etc.).

The main issue here being that, if we implement according to 2.3 this would seem to indicate that sending in {"contentType":"application/json", "data":["list"]} would be invalid (as data could only be a map or a string), and we should return an error upon receiving an event with this data section. 3.1 seems much more intuitive (and in implementation, it’s quite smooth, for ahem languages without generics 😉, however seems in conflict with the type system defined in 2.3. Additionally, it seems wrong to reject events that provide a valid json value in the data section (and 3.1 seems happy with this). Hopefully, this is just a matter of clarifying and this isn’t just me being wrong on the internet again.

A possible solution: at present, the grammar seems somewhat ambiguous with the Map type being rigidly defined as a json map, that could also be an object. In 2.3 the wording is around String, Binary, or Map where if 3.1 is the correct way to implement things, this could easily say String, Binary or Object instead, and since an Object can have a Map production this makes complete sense – as opposed to a Map which doesn’t really have the entire Object production, as Object is defined, e.g. a Map can’t have a value of false – even though 2.3 seems to imply a Map could be an Object. To put in simpler terms, it seems like we should have things the other way around and say it’s a String, Binary or Object where Object may be a Map, vs. the current other way around. Perhaps, I am way off base, too 😃

Thanks again!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
clemensvcommented, Nov 28, 2018

“JSON object” should indeed refer to JSON value in order to permit arrays. So, yes, we should change that.

0reactions
rdallmancommented, Nov 29, 2018

spec.md LGTM now, the type is very clear. thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 7159: The JavaScript Object Notation (JSON) Data ...
JSON defines a small set of formatting rules for the portable representation of structured data. This document removes inconsistencies with other ...
Read more >
Schema for Violations (constraint_violations.json file)
If the data types in the current execution are not the same as in the baseline dataset, this violation is flagged. During the...
Read more >
A Media Type for Describing JSON Documents - JSON Schema
JSON Schema is a JSON media type for defining the structure of JSON data. ... Schema document and resource concepts will be clarified...
Read more >
Working with JSON data in Google Standard SQL | BigQuery
https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud ... This section describes how to use Google Standard SQL to extract values from ...
Read more >
An Introduction to JSON | DigitalOcean
It is important to keep in mind that though they appear similar, a JSON object is not the same format as a JavaScript...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found