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

Fix handling of array/object enum samples in JSON 0.6 serialisation #225

Merged
merged 2 commits into from
Mar 15, 2019

Conversation

kylef
Copy link
Member

@kylef kylef commented Mar 15, 2019

Given the following API Element:

{
  "element": "enum",
  "meta": {
    "id": {
      "element": "string",
      "content": "Schema"
    }
  },
  "attributes": {
    "enumerations": {
      "element": "array",
      "content": [
        {
          "element": "string"
        },
        {
          "element": "number"
        },
        {
          "element": "boolean"
        },
        {
          "element": "object"
        },
        {
          "element": "array"
        }
      ]
    },
    "samples": {
      "element": "array",
      "content": [
        {
          "element": "object",
          "content": [
            {
              "element": "member",
              "content": {
                "key": {
                  "element": "string",
                  "content": "message"
                },
                "value": {
                  "element": "string",
                  "content": "Hello World"
                }
              }
            }
          ]
        }
      ]
    }
  }
}

Which is derved from OAS 3 document schema of:

Schema:
  example:
    message: 'Hello World'

Since the schema isn't limited to any type, it's effectively an "enum" of all types, with an example value for an object with values.

This would previously raise the following exception:

TypeError: Cannot read property 'remove' of undefined
    at samples.forEach (/Users/kyle/Projects/apiaryio/api-elements.js/node_modules/minim/lib/serialisers/JSON06Serialiser.js:125:33)
    at content.forEach (/Users/kyle/Projects/apiaryio/api-elements.js/node_modules/minim/lib/primitives/ArrayElement.js:174:29)
    at Array.forEach (<anonymous>)
    at ArrayElement.forEach (/Users/kyle/Projects/apiaryio/api-elements.js/node_modules/minim/lib/primitives/ArrayElement.js:173:18)
    at JSON06Serialiser.enumSerialiseAttributes (/Users/kyle/Projects/apiaryio/api-elements.js/node_modules/minim/lib/serialisers/JSON06Serialiser.js:124:13)
    at JSON06Serialiser.serialise (/Users/kyle/Projects/apiaryio/api-elements.js/node_modules/minim/lib/serialisers/JSON06Serialiser.js:25:31)
    at JSON06Serialiser.serialiseContent (/Users/kyle/Projects/apiaryio/api-elements.js/node_modules/minim/lib/serialisers/JSON06Serialiser.js:281:19)
    at JSON06Serialiser.dataStructureSerialiseContent (/Users/kyle/Projects/apiaryio/api-elements.js/node_modules/minim/lib/serialisers/JSON06Serialiser.js:105:18)
    at JSON06Serialiser.serialise (/Users/kyle/Projects/apiaryio/api-elements.js/node_modules/minim/lib/serialisers/JSON06Serialiser.js:53:67)
    at Array.map (<anonymous>)

@kylef kylef added the bug label Mar 15, 2019
samples.forEach((sample) => {
sample.content.attributes.remove('typeAttributes');
if (sample.content && sample.content.element) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix for the particular exception, the "content" of the sample (object) is an array in this case and thus does not have "attributes" (which returns undefined). The fix here is adding a "cheap" check if the sample's content exists, and it is an element.

@@ -132,7 +135,13 @@ module.exports = class JSON06Serialiser extends JSONSerialiser {
samples.unshift(element.content);
}

samples = samples.map(sample => new this.namespace.elements.Array([sample.content]));
samples = samples.map((sample) => {
if (sample instanceof this.namespace.elements.Array) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once I fixed the exception, I discovered the output from the serialiser to be incorrect, we was taking the members (sample.content) and wrapping them in an array. Instead I've added a check that if sample is already an array type (such as an object element) then we will just wrap the entire element in an array and not it's content (element).

@pksunkara pksunkara merged commit 76949b5 into master Mar 15, 2019
@pksunkara pksunkara deleted the kylef/remove-undefined branch March 15, 2019 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants