dredd icon indicating copy to clipboard operation
dredd copied to clipboard

Required properties inside variable property names

Open c0 opened this issue 10 years ago • 8 comments

I have an API that returns keys based on ID and Dredd is reporting an error that doesn't match what it expects.

There's also a lot of repos that dredd uses. Please direct me if I should file this issue somewhere else.

For example:

{
  "todosById": {
    "8": {
      "text": "Prepare Dinner"
    }
  }
}

I can express this in MSON with:


+ Response 200 (application/json)

    + Attributes

        + todosById
            + *8*
                + text: Prepare Dinner (string, required)

Dredd output

fail: body: At '/todosById/id' Missing required property: id
body: At '/todosById/text' Missing required property: text

expected:
headers:
    Content-Type: application/json

body:
{
  "todosById": {
    "id": {
      "name": "Prepare Dinner"
    }
  }
}
statusCode: 200
bodySchema: {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "todosById": {
      "$ref": "#/definitions/id"
    }
  },
  "definitions": {
    "id": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ]
    }
  }
}

c0 avatar Jun 15 '16 16:06 c0

I'm sorry we didn't reply, this somehow slipped through our net.

The background of this is that Dredd uses JSON Schema generated from the Attributes syntax, a.k.a. MSON, by the API Blueprint parser, a.k.a. Drafter.

So this is more of a feature request to Drafter. However, Dredd allows you to override the generated JSON Schema by specifying a + Schema section - see related docs. You can write your own schema validating the payload in exactly the way you want. Also, you can have your own, programmatic assertions by using hooks.

@c0 Do you think those approaches would work for you?

Also, if you would be able to come up with a general approach on how to validate the structure in JSON Schema, share it with Drafter creators and I'm sure they'll gladly implement it.

honzajavorek avatar Jul 27 '16 19:07 honzajavorek

@c0 Actually, reading your post once again, I see I answered a bit of a different question than you actually asked. Your test run is failing and it shouldn't. I wonder what's current support for MSON's *arbitrary* properties in the parser...

Are you sure the schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "todosById": {
      "$ref": "#/definitions/id"
    }
  },
  "definitions": {
    "id": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ]
    }
  }
}

is something generated from

        + todosById
            + *8*
                + text: Prepare Dinner (string, required)

or is it something you wrote on your own to the + Schema section?

honzajavorek avatar Jul 27 '16 19:07 honzajavorek

If I try to parse

FORMAT: 1A

# Beehive API

## Honey [/honey]

### Retrieve [GET]

+ Response 200 (application/json)

    + Attributes

        + todosById
            + *8*
                + text: Prepare Dinner (string, required)

with the latest drafter, I get

JSON

{
  "todosById": {
    "8": {
      "text": "Prepare Dinner"
    }
  }
}

JSON Schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "todosById": {
      "$ref": "#/definitions/8"
    }
  },
  "definitions": {
    "8": {
      "type": "object",
      "properties": {
        "text": {
          "type": "string"
        }
      },
      "required": [
        "text"
      ]
    }
  }
}

I think the schema is incorrect, because the property is variable and the schema looks like it forces you to have a property called 8 exactly. In my opinion this is a bug.

honzajavorek avatar Jul 27 '16 19:07 honzajavorek

@honzajavorek How would the schema work here?

pksunkara avatar Oct 10 '16 11:10 pksunkara

@pksunkara see https://github.com/apiaryio/boutique.js/blob/master/test/formats/samples-json-schema-v4/variable-property.json

w-vi avatar Oct 10 '16 11:10 w-vi

@c0 I learned the issue got addressed in the latest parser, so soon fix should be also available in Dredd.

honzajavorek avatar Oct 14 '16 17:10 honzajavorek

@c0 Did the fix resolved your issue?

honzajavorek avatar Nov 03 '16 18:11 honzajavorek

@c0 I didn't hear back from you. I'll assume this is fixed now. Thanks very much for filing this and helping to debug the issue! 👍 Really appreciated. Feel free to get back and re-open this should you ever have any problem with this.

honzajavorek avatar Mar 08 '17 15:03 honzajavorek