-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for OpenAPI 3 response.content with fallback
- Loading branch information
Showing
7 changed files
with
372 additions
and
31 deletions.
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
src/NSwag.Core.Tests/Serialization/MediaTypesSerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.