Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

dt2js: wrong JSON schema from RAML Array of union type #15

Closed
wolframkleis opened this issue Sep 8, 2017 · 2 comments
Closed

dt2js: wrong JSON schema from RAML Array of union type #15

wolframkleis opened this issue Sep 8, 2017 · 2 comments
Milestone

Comments

@wolframkleis
Copy link

In RAML I have an array type with item type "Device" that is the union of 2 other types "Phone" and "Notebook". (types are from example in raml spec only array is new)
My array type seems to be converted to an JSON schema that does not describe the same data.

This is the RAML:

#%RAML 1.0
title: My API With Types
types:
  Phone:
    type: object
    properties:
      manufacturer:
        type: string
      numberOfSIMCards:
        type: number
      kind: string
  Notebook:
    type: object
    properties:
      manufacturer:
        type: string
      numberOfUSBPorts:
        type: number
      kind: string
  Device:
    type: Phone | Notebook
  Devices: Device[]

For type "Devices", dt2js produces this JSON Schema :

{
  "anyOf": [
    {
      "type": "array",
      "items": {
        "properties": {
          "manufacturer": {
            "type": "string"
          },
          "numberOfSIMCards": {
            "type": "number"
          },
          "kind": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "type": "object",
        "required": [
          "manufacturer",
          "numberOfSIMCards",
          "kind"
        ]
      }
    },
    {
      "type": "array",
      "items": {
        "properties": {
          "manufacturer": {
            "type": "string"
          },
          "numberOfUSBPorts": {
            "type": "number"
          },
          "kind": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "type": "object",
        "required": [
          "manufacturer",
          "numberOfUSBPorts",
          "kind"
        ]
      }
    }
  ],
  "type": "object",
  "$schema": "http://json-schema.org/draft-04/schema#"
}

I think the type should be array and not object, and "any-of" should be on items level.

Shouldn't the schema look like this instead?

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "anyOf": [
      {
        "properties": {
          "manufacturer": {
            "type": "string"
          },
          "numberOfSIMCards": {
            "type": "number"
          },
          "kind": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "type": "object",
        "required": [
          "manufacturer",
          "numberOfSIMCards",
          "kind"
        ]
      },
      {
        "properties": {
          "manufacturer": {
            "type": "string"
          },
          "numberOfUSBPorts": {
            "type": "number"
          },
          "kind": {
            "type": "string"
          }
        },
        "additionalProperties": true,
        "type": "object",
        "required": [
          "manufacturer",
          "numberOfUSBPorts",
          "kind"
        ]
      }
    ]
  }
}
@jstoiko jstoiko added this to the v0.1.2 milestone Dec 13, 2017
@jstoiko
Copy link
Contributor

jstoiko commented Dec 13, 2017

@wolframkleis: did you ever overcome this? seems like there needs to be a bit of extra logic here.

@jstoiko jstoiko removed this from the v0.1.2 milestone Jan 4, 2018
@jstoiko
Copy link
Contributor

jstoiko commented Jan 9, 2018

@wolframkleis: not sure if you had time to test @brevity's PR (#29). If you haven't, feel free to do so and give us your feedback here. If I don't hear from you by tomorrow, I'll go ahead and merge this. Thanks again for reporting the issue!

@jstoiko jstoiko added this to the v0.1.3 milestone Feb 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants