Skip to content

Updated Examples #1035

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

Merged
merged 6 commits into from
Apr 21, 2017
Merged

Updated Examples #1035

merged 6 commits into from
Apr 21, 2017

Conversation

darrelmiller
Copy link
Member

@darrelmiller darrelmiller commented Apr 11, 2017

This proposal attempts to address the issues raised in #488

The "Example Object" has been updated. This object now supports the following scenarios:
a) multiple examples are required,
b) when short summary or long description is needed, or
c) when an example needs to be reused using a reference object.

examples:
  cat:
    summary: Big cat example
    value:
      name: Puma
      id: 1
  dog:
    summary: Family Dog
    description: This is the story about Ferguson the dog ...
    value:
      name: Ferguson
      id: 2
  frog:
    $ref: "#/components/examples/frog"

For simpler scenarios where only one example is required, you can inline a single example.

example:
    name: Ferguson
    id: 2

For scenarios where the example is for a media type that is not JSON or YAML the value can be externalized using the externalValue property instead of the value property. The value retrieved when dereferencing this URL must match the media type/ encoding associated with described example object.

  'application/xml':
    examples:
      user:
        summary: User Example in XML
        externalValue: http://foo.bar/examples/user-example.xml

For Content Objects, Parameter Objects and Header Objects, you can use either a inline example or a map of described example objects called examples , but not both. Schema objects only support inline example as in OpenAPI V2.

@whitlockjc
Copy link
Member

Does this address a specific issue?

@darrelmiller
Copy link
Member Author

@whitlockjc Yes, #488

@DavidBiesack
Copy link
Contributor

I recommend simpler terminology -- Example Object for example' and Examples Objectrather thanDescribed Example Objectfor this new object. (It's plural since it is a map, i.e. a container of multiple examples). The two are distinct, and I think they should be mutually exclusive (asexampleandexamples` are now.) This just replaces the examples array with an Examples object.

@darrelmiller
Copy link
Member Author

darrelmiller commented Apr 11, 2017

@DavidBiesack But the "described example object" that is mapped by the "examples" object is not the same object as is used in "example". That's why we have the new name. It has the summary, description and value property. There isn't much benefit in creating a distinct object for the map of "described example objects" which is why I just identified it as a Map. This is a convention we started using in Components.
It might be possible to not use "example object" in the "example" property and just use "any" as is done in the schema object and then we could reclaim "example object" to be what I have called "described example object". The only downside there is we lose the place to document the fact that the "example object" is a literal value or a string that contains an escaped example.

Copy link
Contributor

@DavidBiesack DavidBiesack left a comment

Choose a reason for hiding this comment

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

I would prefer to rename the existing Example Object since it is not an object (in the JSON sense), it is really just an Example Value (may be a non-object number, string, boolean, array). Rename your Described Example Object as Example Object. Then your Examples Object could be a map of Example Objects each of which has summary, description, value, x-^ . Further, any $ref for an example must be only within examples Examples Object; one cannot use $ref in an example Also, consider changing valueUri to use $ref

versions/3.0.md Outdated
@@ -53,12 +53,12 @@ Additional utilities can also take advantage of the resulting files, such as tes
- [Callback Object](#callbackObject)
- [Headers Object](#headersObject)
- [Example Object](#exampleObject)
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest renaming Example Object as Example Value since it is not an JSON object

Copy link
Member Author

Choose a reason for hiding this comment

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

That seems like a reasonable approach. I must admit, I did like the "Described Example" moniker but Example Value is more accurate than Example Object for the current usage.

Copy link
Contributor

Choose a reason for hiding this comment

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

(Funny.. I felt the Described Example moniker was slightly awkward.)

Copy link
Member Author

Choose a reason for hiding this comment

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

It's ok, I'm not too emotionally attached to it :-)

Copy link
Member

Choose a reason for hiding this comment

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

If the valueUrl concept survives, consider whether having a name that is more similar to externalDocs which has some similarities here (non-$ref style references), like externalValue or externalExample

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

versions/3.0.md Outdated
@@ -53,12 +53,12 @@ Additional utilities can also take advantage of the resulting files, such as tes
- [Callback Object](#callbackObject)
- [Headers Object](#headersObject)
- [Example Object](#exampleObject)
- [Described Example Object](#describedExampleObject)
Copy link
Contributor

Choose a reason for hiding this comment

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

... then this can be Example Object

versions/3.0.md Outdated
@@ -408,7 +408,7 @@ Field Name | Type | Description
<a name="componentsSchemas"></a> schemas | Map[`string`, [Schema Object](#schemaObject)] | An object to hold reusable [Schema Objects](#schemaObject).
<a name="componentsResponses"></a> responses | Map[`string`, [Response Object](#responseObject)] | An object to hold reusable [Response Objects](#responseObject).
<a name="componentsParameters"></a> parameters | Map[`string`, [Parameter Object](#parameterObject)] | An object to hold reusable [Parameter Objects](#parameterObject).
<a name="componentsExamples"></a> examples | Map[`string`, [Example Object](#exampleObject)] | An object to hold reusable [Example Objects](#exampleObject).
<a name="componentsExamples"></a> examples | Map[`string`, [Described Example Object](#describedExampleObject)] | An object to hold reusable [Described Example Objects](#describedExampleObject).
Copy link
Contributor

Choose a reason for hiding this comment

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

... then this could be Map[string, Example Object]

versions/3.0.md Outdated
@@ -938,8 +938,8 @@ Field Name | Type | Description
<a name="parameterExplode"></a>explode | `boolean` | When this is true, parameter values of type `array` or `object` generate separate parameters for each value of the array, or key-value-pair of the map. For other types of parameters this property has no effect. When [`style`](#parameterStyle) is `form`, the default value is `true`. For all other styles, the default value is `false`.
<a name="parameterAllowReserved"></a>allowReserved | `boolean` | Determines whether the parameter value SHOULD allow reserved characters, as defined by [RFC3986](https://tools.ietf.org/html/rfc3986#section-2.2) `:/?#[]@!$&'()*+,;=` to be included without percent-encoding. This property only applies to parameters with an `in` value of `query`. The default value is `false`.
<a name="parameterSchema"></a>schema | [Schema Object](#schemaObject) \| [Reference Object](#referenceObject)] | The schema defining the type used for the parameter.
<a name="parameterExamples"></a>examples | [[Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the content type. Each example in the Examples array SHOULD be in the correct format as specified parameter encoding. The `examples` object is mutually exclusive to the `example` object. Furthermore, if referencing a `schema` which contains an example, the `examples` value SHALL _override_ the example provided by the schema.
<a name="parameterExample"></a>example | [Example Object](#exampleObject) \| [Reference Object](#referenceObject) | Example of the content type. The example object SHOULD be in the correct format as specified in the parameter encoding. The `example` object is mutually exclusive to the `examples` object. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the example provided by the schema.
<a name="parameterExample"></a>example | [Example Object](#exampleObject) | Example of the media type. The example object SHOULD be in the correct format as specified in the parameter encoding. The `example` object is mutually exclusive to the `examples` object. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the the example provided by the schema.
Copy link
Contributor

Choose a reason for hiding this comment

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

suggest changing to Example Value and "example value SHOULD ..."

versions/3.0.md Outdated
<a name="parameterExamples"></a>examples | [[Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the content type. Each example in the Examples array SHOULD be in the correct format as specified parameter encoding. The `examples` object is mutually exclusive to the `example` object. Furthermore, if referencing a `schema` which contains an example, the `examples` value SHALL _override_ the example provided by the schema.
<a name="parameterExample"></a>example | [Example Object](#exampleObject) \| [Reference Object](#referenceObject) | Example of the content type. The example object SHOULD be in the correct format as specified in the parameter encoding. The `example` object is mutually exclusive to the `examples` object. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the example provided by the schema.
<a name="parameterExample"></a>example | [Example Object](#exampleObject) | Example of the media type. The example object SHOULD be in the correct format as specified in the parameter encoding. The `example` object is mutually exclusive to the `examples` object. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the the example provided by the schema.
<a name="parameterExamples"></a>examples | Map[ `string`, [Described Example Object](#describedExampleObject) \| [Reference Object](#referenceObject)] | Described Examples of the media type. Each described example SHOULD contain a value in the correct format as specified in the parameter encoding. The `examples` object is mutually exclusive to the `example` object. Furthermore, if referencing a `schema` which contains an example, the `examples` value SHALL _override_ the example provided by the schema.
Copy link
Contributor

Choose a reason for hiding this comment

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

Map[ string, Example Object ...

@@ -1822,9 +1894,9 @@ X-Rate-Limit-Reset:

#### <a name="exampleObject"></a>Example Object
Copy link
Contributor

Choose a reason for hiding this comment

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

becomes <a name="exampleValue"></a>Example Value

versions/3.0.md Outdated

##### Example Example
##### Example Object Example
Copy link
Contributor

Choose a reason for hiding this comment

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

becomes Example Value Example

versions/3.0.md Outdated
@@ -1848,6 +1920,84 @@ breed: Mixed

```

#### <a name="describedExampleObject"></a>Described Example Object
Copy link
Contributor

Choose a reason for hiding this comment

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

becomes <a name="exampleObject"></a>Example Object

versions/3.0.md Outdated
##### Fixed Fields
Field Name | Type | Description
---|:---:|---
<a name="describedExampleSummary"></a>summary | `string` | Short description for the example.
Copy link
Contributor

Choose a reason for hiding this comment

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

drop "described" throughout

versions/3.0.md Outdated
<a name="describedExampleSummary"></a>summary | `string` | Short description for the example.
<a name="describedExampleDescription"></a>description | `string` | Long description for the example.
<a name="describedExampleValue"></a>value | [Example Object](#exampleObject) | Embedded literal example. The `value` field and `valueUrl` field are mutually exclusive.
<a name="describedExampleValueUrl"></a>valueUrl | `string` | A URL that points to the literal example. This provides the ability to reference examples that cannot easily be included in JSON or YAML documents. The `value` field and `valueUrl` field are mutually exclusive.
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest using $ref : uri instead of valueUri.
This way, it can also be a pointer to a value elsewhere in the OpenAPI definition, or it can be an external resource

Copy link
Member Author

@darrelmiller darrelmiller Apr 12, 2017

Choose a reason for hiding this comment

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

It can't be done like that as then we would lose the summary and description properties. The alternative would be this:

examples:
  foo: 
    summary: this is a referenced example value
    value:
      $ref: http://example.com/reuse.json#/example/one

The downside to this, is that you can no longer reference example values that are not JSON or YAML. Based on the conversation with @fehguy the other day, I am recommending that we add wording to the reference object definition that $ref can only point to JSON or YAML content.
By using valueUrl we can have something special for examples that may be other formats. (and later schemas, but shhhhhh, don't say that out loud).

The other reason I decided against using reference objects in the value property is that you can already reference described examples either in the current document or externally, so why do we also need to reference the value. If you are going to re-use, you would likely want the summary/description too.

@darrelmiller darrelmiller changed the title Described Examples Updated Examples Apr 16, 2017
@darrelmiller
Copy link
Member Author

Changed made based on TDC review.

  • Described Example Object becomes Example Object
  • The old example goes away and we now just use Any for the type of example.
  • valueUrl has been renamed to externalValue.

@fehguy
Copy link
Contributor

fehguy commented Apr 16, 2017

Looks good to me!

Copy link
Contributor

@raymondfeng raymondfeng left a comment

Choose a reason for hiding this comment

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

LGTM

versions/3.0.md Outdated
type: string
examples:
name:
$ref: http://example.org/petapi-examples/OpenApi.json#/components/examples/name-example
Copy link
Member

Choose a reason for hiding this comment

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

openapi.json

Copy link
Member

@webron webron left a comment

Choose a reason for hiding this comment

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

Overall, looks good. Some of my comments are nitpicking, one or two are note. Other than that, :shipit:.

versions/3.0.md Outdated
name:
$ref: http://example.org/petapi-examples/OpenApi.json#/components/examples/name-example

# in a request body, note the plural `examples` as the Content-Type is set to `*`:
Copy link
Member

Choose a reason for hiding this comment

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

The Content-Type is set to *?

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be */*?

Copy link
Member

Choose a reason for hiding this comment

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

It should be. The comment itself doesn't seem in place though because nothing in the example describes a wildcard content type.

versions/3.0.md Outdated
@@ -2412,6 +2532,7 @@ schemas:
$ref: http://foo.bar#/examples/address-example.json
```

>>>>>>> refs/remotes/origin/OpenAPI.next
Copy link
Member

Choose a reason for hiding this comment

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

I don't think so...

Copy link
Member

Choose a reason for hiding this comment

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

Great job merging your conflict @darrelmiller. 🥇 (joke of course)

versions/3.0.md Outdated
"name": "Ferguson",
"id": 2
}]
"examples": {
Copy link
Member

Choose a reason for hiding this comment

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

If we dropped examples, this needs to change.

Copy link
Member

Choose a reason for hiding this comment

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

Why would we support example and examples elsewhere but not here? Was removing this intentional? It doesn't seem to be because on line 2812 you use examples.

Copy link
Member

Choose a reason for hiding this comment

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

The idea behind removing examples was that unlike other places, there's no content here and no media types described. Providing multiple examples for a model is out of context and doesn't serve a lot of value. You'd need to combine the schema with a media type to define how it's actually serialized. You could argue that even the example should be dropped for that reason.

versions/3.0.md Outdated
id: 1
- name: Ferguson
id: 2
cat:
Copy link
Member

Choose a reason for hiding this comment

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

same as above

versions/3.0.md Outdated
@@ -938,8 +937,8 @@ Field Name | Type | Description
<a name="parameterExplode"></a>explode | `boolean` | When this is true, parameter values of type `array` or `object` generate separate parameters for each value of the array, or key-value-pair of the map. For other types of parameters this property has no effect. When [`style`](#parameterStyle) is `form`, the default value is `true`. For all other styles, the default value is `false`.
<a name="parameterAllowReserved"></a>allowReserved | `boolean` | Determines whether the parameter value SHOULD allow reserved characters, as defined by [RFC3986](https://tools.ietf.org/html/rfc3986#section-2.2) `:/?#[]@!$&'()*+,;=` to be included without percent-encoding. This property only applies to parameters with an `in` value of `query`. The default value is `false`.
<a name="parameterSchema"></a>schema | [Schema Object](#schemaObject) \| [Reference Object](#referenceObject)] | The schema defining the type used for the parameter.
<a name="parameterExamples"></a>examples | [[Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the content type. Each example in the Examples array SHOULD be in the correct format as specified parameter encoding. The `examples` object is mutually exclusive to the `example` object. Furthermore, if referencing a `schema` which contains an example, the `examples` value SHALL _override_ the example provided by the schema.
<a name="parameterExample"></a>example | [Example Object](#exampleObject) \| [Reference Object](#referenceObject) | Example of the content type. The example object SHOULD be in the correct format as specified in the parameter encoding. The `example` object is mutually exclusive to the `examples` object. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the example provided by the schema.
<a name="parameterExample"></a>example | Any | Example of the media type. The example object SHOULD be in the correct format as specified in the parameter encoding. The `example` object is mutually exclusive to the `examples` object. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the the example provided by the schema. To represent examples of media types that cannot naturally represented in JSON or YAML, a string value can be used to contain the example with escaping where necessary.
Copy link
Member

Choose a reason for hiding this comment

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

To me, "correct format" makes sense but should we be more explicit, something to the effect of "it should adhere to the schema specified in the parameter encoding"?

Copy link
Member Author

Choose a reason for hiding this comment

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

There are two issues here. If the parameter encoding says XML, then the example needs to be XML. That's what I meant by correct format. Saying that the example should conform to the schema is a different requirement. Let me try and come up with words that say both.

Copy link
Member

Choose a reason for hiding this comment

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

"it should make sense, don't do anything crazy"

versions/3.0.md Outdated
<a name="parameterExamples"></a>examples | [[Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the content type. Each example in the Examples array SHOULD be in the correct format as specified parameter encoding. The `examples` object is mutually exclusive to the `example` object. Furthermore, if referencing a `schema` which contains an example, the `examples` value SHALL _override_ the example provided by the schema.
<a name="parameterExample"></a>example | [Example Object](#exampleObject) \| [Reference Object](#referenceObject) | Example of the content type. The example object SHOULD be in the correct format as specified in the parameter encoding. The `example` object is mutually exclusive to the `examples` object. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the example provided by the schema.
<a name="parameterExample"></a>example | Any | Example of the media type. The example object SHOULD be in the correct format as specified in the parameter encoding. The `example` object is mutually exclusive to the `examples` object. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the the example provided by the schema. To represent examples of media types that cannot naturally represented in JSON or YAML, a string value can be used to contain the example with escaping where necessary.
<a name="parameterExamples"></a>examples | Map[ `string`, [Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the media type. Each example SHOULD contain a value in the correct format as specified in the parameter encoding. The `examples` object is mutually exclusive to the `example` object. Furthermore, if referencing a `schema` which contains an example, the `examples` value SHALL _override_ the example provided by the schema.
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

@@ -1128,20 +1127,38 @@ A request body with a referenced model definition.
"schema": {
"$ref": "#/components/schemas/User"
},
"examples": [ "http://foo.bar/examples/user-example.json" ]
"examples": {
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the value. Since examples is no longer an array if items, what is the significance of the map key?

Copy link
Member

Choose a reason for hiding this comment

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

It appears to be a free-form description/label/name but the intent is not explained in the "Examples Object" description.

Copy link
Contributor

Choose a reason for hiding this comment

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

The key allows referencing/reusing the specific example in a $ref

@whitlockjc
Copy link
Member

I forgot to start a review, my bad. Review done, comments above.

@darrelmiller darrelmiller merged commit 1b9a5c1 into OpenAPI.next Apr 21, 2017
@darrelmiller darrelmiller deleted the dm/examples-syntax branch April 21, 2017 16:53
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

Successfully merging this pull request may close these issues.

7 participants