Skip to content

Commit

Permalink
Added support for OpenAPI 3 response.content with fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Mar 21, 2018
1 parent 937f0a0 commit ea63ef2
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 31 deletions.
189 changes: 189 additions & 0 deletions src/NSwag.Core.Tests/Serialization/MediaTypesSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using NJsonSchema;
using Xunit;

namespace NSwag.Core.Tests.Serialization
{
public class MediaTypesSerializationTests
{
[Fact]
public void When_when_response_schema_and_example_is_set_then_it_is_serialized_correctly_in_Swagger()
{
//// Arrange
var document = CreateDocument(JsonObjectType.String);

//// Act
var json = document.ToJson(SchemaType.Swagger2);

//// Assert
Assert.Contains(
@" ""paths"": {
""/foo"": {
""get"": {
""operationId"": ""foo"",
""responses"": {
""200"": {
""description"": """",
""schema"": {
""type"": ""string""
},
""examples"": 123
}
}
}
}
}
}", json);
}

[Fact]
public void When_when_response_schema_and_example_is_set_then_it_is_serialized_correctly_in_OpenApi()
{
//// Arrange
var document = CreateDocument(JsonObjectType.String);

//// Act
var json = document.ToJson(SchemaType.OpenApi3);

//// Assert
Assert.Equal(
@"{
""openapi"": ""3.0"",
""info"": {
""title"": """",
""version"": """"
},
""paths"": {
""/foo"": {
""get"": {
""operationId"": ""foo"",
""responses"": {
""200"": {
""description"": """",
""content"": {
""application/json"": {
""example"": 123,
""schema"": {
""type"": ""string""
}
}
}
}
}
}
}
},
""components"": {}
}", json);
}

[Fact]
public void When_when_response_schema_and_example_is_set_as_file_then_it_is_serialized_correctly_in_Swagger()
{
//// Arrange
var document = CreateDocument(JsonObjectType.File);

//// Act
var json = document.ToJson(SchemaType.Swagger2);

//// Assert
Assert.Contains(
@" ""paths"": {
""/foo"": {
""get"": {
""operationId"": ""foo"",
""responses"": {
""200"": {
""description"": """",
""schema"": {
""type"": ""file""
},
""examples"": 123
}
}
}
}
}
}", json);
}

[Fact]
public void When_when_response_schema_and_example_is_set_as_file_then_it_is_serialized_correctly_in_OpenApi()
{
//// Arrange
var document = CreateDocument(JsonObjectType.File);

//// Act
var json = document.ToJson(SchemaType.OpenApi3);

//// Assert
Assert.Equal(
@"{
""openapi"": ""3.0"",
""info"": {
""title"": """",
""version"": """"
},
""paths"": {
""/foo"": {
""get"": {
""operationId"": ""foo"",
""responses"": {
""200"": {
""description"": """",
""content"": {
""application/octet-stream"": {
""example"": 123,
""schema"": {
""type"": ""file""
}
}
}
}
}
}
}
},
""components"": {}
}", json);
}


private static SwaggerDocument CreateDocument(JsonObjectType type)
{
var document = new SwaggerDocument
{
Paths =
{
{
"/foo",
new SwaggerPathItem
{
{
SwaggerOperationMethod.Get,
new SwaggerOperation
{
Responses =
{
{
"200",
new SwaggerResponse
{
Examples = 123,
Schema = new JsonSchema4
{
Type = type
},
}
}
}
}
}
}
}
}
};

return document;
}
}
}
20 changes: 20 additions & 0 deletions src/NSwag.Core.Tests/Serialization/ServersSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ namespace NSwag.Core.Tests.Serialization
{
public class ServersSerializationTests
{
[Fact]
public async Task When_document_is_empty_then_serialized_correctly_in_Swagger()
{
//// Arrange
var document = new SwaggerDocument();

//// Act
var json = document.ToJson(SchemaType.Swagger2);

//// Assert
Assert.Equal(
@"{
""swagger"": ""2.0"",
""info"": {
""title"": """",
""version"": """"
}
}", json);
}

[Fact]
public async Task When_schema_is_added_to_definitions_then_it_is_serialized_correctly_in_Swagger()
{
Expand Down
49 changes: 30 additions & 19 deletions src/NSwag.Core/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,21 @@ public class OpenApiComponents
/// <param name="document"></param>
public OpenApiComponents(SwaggerDocument document)
{
SecuritySchemes = new Dictionary<string, SwaggerSecurityScheme>();
Examples = new Dictionary<string, OpenApiExample>();

var schemas = new ObservableDictionary<string, JsonSchema4>();
schemas.CollectionChanged += (sender, args) =>
{
foreach (var path in Schemas.Values)
foreach (var path in schemas.Values)
path.Parent = document;
};
Schemas = schemas;

var headers = new ObservableDictionary<string, JsonSchema4>();
headers.CollectionChanged += (sender, args) =>
var responses = new ObservableDictionary<string, SwaggerResponse>();
responses.CollectionChanged += (sender, args) =>
{
foreach (var path in Schemas.Values)
foreach (var path in Responses.Values)
path.Parent = document;
};
Headers = headers;
Responses = responses;

var parameters = new ObservableDictionary<string, SwaggerParameter>();
parameters.CollectionChanged += (sender, args) =>
Expand All @@ -47,30 +44,32 @@ public OpenApiComponents(SwaggerDocument document)
};
Parameters = parameters;

var responses = new ObservableDictionary<string, SwaggerResponse>();
responses.CollectionChanged += (sender, args) =>
Examples = new Dictionary<string, OpenApiExample>();

var headers = new ObservableDictionary<string, JsonSchema4>();
headers.CollectionChanged += (sender, args) =>
{
foreach (var path in Responses.Values)
foreach (var path in headers.Values)
path.Parent = document;
};
Responses = responses;
Headers = headers;

SecuritySchemes = new Dictionary<string, SwaggerSecurityScheme>();
Links = new Dictionary<string, OpenApiLink>();
Callbacks = new Dictionary<string, OpenApiCallback>();
}

/// <summary>Gets or sets the types.</summary>
[JsonProperty(PropertyName = "schemas", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, JsonSchema4> Schemas { get; }

/// <summary>Gets or sets the parameters which can be used for all operations.</summary>
[JsonProperty(PropertyName = "parameters", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, SwaggerParameter> Parameters { get; }

/// <summary>Gets or sets the responses which can be used for all operations.</summary>
[JsonProperty(PropertyName = "responses", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, SwaggerResponse> Responses { get; }

/// <summary>Gets or sets the security definitions.</summary>
[JsonProperty(PropertyName = "securitySchemes", DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<string, SwaggerSecurityScheme> SecuritySchemes { get; }
/// <summary>Gets or sets the parameters which can be used for all operations.</summary>
[JsonProperty(PropertyName = "parameters", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, SwaggerParameter> Parameters { get; }

/// <summary>Gets or sets the headers.</summary>
[JsonProperty(PropertyName = "examples", DefaultValueHandling = DefaultValueHandling.Ignore)]
Expand All @@ -79,5 +78,17 @@ public OpenApiComponents(SwaggerDocument document)
/// <summary>Gets or sets the types.</summary>
[JsonProperty(PropertyName = "headers", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, JsonSchema4> Headers { get; }

/// <summary>Gets or sets the security definitions.</summary>
[JsonProperty(PropertyName = "securitySchemes", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, SwaggerSecurityScheme> SecuritySchemes { get; }

/// <summary>Gets or sets the security definitions.</summary>
[JsonProperty(PropertyName = "links", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiLink> Links { get; }

/// <summary>Gets or sets the security definitions.</summary>
[JsonProperty(PropertyName = "callbacks", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, OpenApiCallback> Callbacks { get; }
}
}
52 changes: 52 additions & 0 deletions src/NSwag.Core/OpenApiLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//-----------------------------------------------------------------------
// <copyright file="OpenApiLink.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using System.Collections.Generic;
using Newtonsoft.Json;
using NJsonSchema.References;

namespace NSwag
{
/// <summary>The Swagger example (OpenAPI only).</summary>
public class OpenApiLink : JsonReferenceBase<OpenApiLink>, IJsonReference
{
/// <summary>Gets or sets the example's description.</summary>
[JsonProperty(PropertyName = "operationRef", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string OperationRef { get; set; }

/// <summary>Gets or sets the example's description.</summary>
[JsonProperty(PropertyName = "operationId", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string OperationId { get; set; }

/// <summary>Gets or sets the example's value.</summary>
[JsonProperty(PropertyName = "parameters", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public IDictionary<string, object> Parameters { get; } = new Dictionary<string, object>();

/// <summary>Gets or sets the example's external value.</summary>
[JsonProperty(PropertyName = "requestBody", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public object RequestBody { get; set; }

/// <summary>Gets or sets the example's external value.</summary>
[JsonProperty(PropertyName = "description", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Description { get; set; }

/// <summary>Gets or sets the server.</summary>
[JsonProperty(PropertyName = "server", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public OpenApiServer Server { get; set; }

#region Implementation of IJsonReference

[JsonIgnore]
IJsonReference IJsonReference.ActualObject => Reference;

[JsonIgnore]
object IJsonReference.PossibleRoot => null;

#endregion
}
}
Loading

0 comments on commit ea63ef2

Please sign in to comment.