Skip to content

Commit

Permalink
Revert of Fix #8 as wrapping the response example in the media type i…
Browse files Browse the repository at this point in the history
…s the correct behavior as per the Swagger spec.
  • Loading branch information
mattfrear committed Sep 15, 2017
1 parent f9185f9 commit fd05335
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public class DeliveryOptionsSearchModelExample : IExamplesProvider
}
```

In the Swagger document, this will populate the request's schema object [example property](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaExample).
The spec for this says:

Field Name | Type | Description
---|:---:|---
example | Any | A free-form property to include an example of an instance for this schema.

## How to use - Response examples

Decorate your methods with the new SwaggerResponseExample attribute:
Expand All @@ -131,6 +138,31 @@ public class CountryExamples : IExamplesProvider
}
}
```

In the Swagger document, this will populate the response's [example object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#exampleObject).
The spec for this says:

Field Pattern | Type | Description
---|:---:|---
{mime type} | Any | The name of the property MUST be one of the Operation produces values (either implicit or inherited). The value SHOULD be an example of what such a response would look like.

Example response for application/json mimetype of a Pet data type:

```js
{
"application/json": {
"name": "Puma",
"type": "Dog",
"color": "Black",
"gender": "Female",
"breed": "Mixed"
}
}
```

Note that this differs from the Request example in that the mime type is a required property on the response example but not so on the request example.


### Known issues
- Although you can add a response examples for each HTTP status code (200, 400, 404 etc), and they will appear in the
swagger.json, they will not display correctly. This is due to an bug in swagger-ui. [Issue 9](https://github.com/mattfrear/Swashbuckle.AspNetCore.Examples/issues/9)
Expand Down
22 changes: 18 additions & 4 deletions Swashbuckle.Examples/ExamplesOperationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void SetRequestModelExamples(Operation operation, SchemaRegistry
if (schemaRegistry.Definitions.ContainsKey(name))
{
var definitionToUpdate = schemaRegistry.Definitions[name];
definitionToUpdate.example = FormatJson(provider, serializerSettings);
definitionToUpdate.example = FormatJson(provider, serializerSettings, false);
}
}
}
Expand All @@ -72,15 +72,29 @@ private static void SetResponseModelExamples(Operation operation, ApiDescription
var provider = (IExamplesProvider)Activator.CreateInstance(attr.ExamplesProviderType);

var serializerSettings = SerializerSettings(controllerSerializerSettings, attr.ContractResolver, attr.JsonConverter);
response.Value.examples = FormatJson(provider, serializerSettings);
response.Value.examples = FormatJson(provider, serializerSettings, true);
}
}
}
}

private static object FormatJson(IExamplesProvider provider, JsonSerializerSettings serializerSettings)
private static object FormatJson(IExamplesProvider provider, JsonSerializerSettings serializerSettings, bool includeMediaType)
{
var examples = provider.GetExamples();
object examples;
if (includeMediaType)
{
examples = new Dictionary<string, object>
{
{
"application/json", provider.GetExamples()
}
};
}
else
{
examples = provider.GetExamples();
}

var jsonString = JsonConvert.SerializeObject(examples, serializerSettings);
var result = JsonConvert.DeserializeObject(jsonString);
return result;
Expand Down
4 changes: 2 additions & 2 deletions Swashbuckle.Examples/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.3.2.0")]
[assembly: AssemblyFileVersion("3.3.2.0")]
[assembly: AssemblyVersion("3.3.3.0")]
[assembly: AssemblyFileVersion("3.3.3.0")]
3 changes: 1 addition & 2 deletions Swashbuckle.Examples/Swashbuckle.Examples.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
<copyright>Copyright 2017</copyright>
<tags>Swagger SwaggerUi Documentation Discovery AspNetWebApi Swashbuckle</tags>
<releaseNotes>
- Fix bug introduced in 3.3.1 - Generic example requests not working Issue #10
- Fix bug where the controller's serializer settings would be overwritten
- Fix bug introduced in 3.3.1. I removed the outer "application/json" mime type from responses, but this is actually the correct according to the Swagger 2.0 spec.
</releaseNotes>
<dependencies>
<dependency id="Swashbuckle.Core" version="5.2.2" />
Expand Down

0 comments on commit fd05335

Please sign in to comment.