-
-
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.
Updated NJS, added OpenAPI components
- Loading branch information
Showing
36 changed files
with
313 additions
and
125 deletions.
There are no files selected for viewing
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
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
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
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
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,67 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using NJsonSchema; | ||
using Xunit; | ||
|
||
namespace NSwag.Core.Tests | ||
{ | ||
public class ComponentsSerializationTests | ||
{ | ||
[Fact] | ||
public async Task When_schema_is_added_to_definitions_then_it_is_serialized_correctly_in_Swagger() | ||
{ | ||
//// Arrange | ||
var document = CreateDocument(); | ||
|
||
//// Act | ||
var json = document.ToJson(SchemaType.Swagger2); | ||
document = await SwaggerDocument.FromJsonAsync(json, schemaType: SchemaType.OpenApi3); | ||
|
||
//// Assert | ||
Assert.Contains("definitions", json); | ||
Assert.DoesNotContain("components", json); | ||
|
||
Assert.True(document.Definitions.ContainsKey("Foo")); | ||
} | ||
|
||
[Fact] | ||
public async Task When_schema_is_added_to_definitions_then_it_is_serialized_correctly_in_OpenApi() | ||
{ | ||
//// Arrange | ||
var document = CreateDocument(); | ||
|
||
//// Act | ||
var json = document.ToJson(SchemaType.OpenApi3); | ||
document = await SwaggerDocument.FromJsonAsync(json, schemaType: SchemaType.OpenApi3); | ||
|
||
//// Assert | ||
Assert.Contains("components", json); | ||
Assert.Contains("schemas", json); | ||
Assert.DoesNotContain("#/definitions/Foo", json); | ||
Assert.DoesNotContain("definitions", json); | ||
|
||
Assert.True(document.Definitions.ContainsKey("Foo")); | ||
} | ||
|
||
private static SwaggerDocument CreateDocument() | ||
{ | ||
var schema = new JsonSchema4 | ||
{ | ||
Type = JsonObjectType.String | ||
}; | ||
|
||
var document = new SwaggerDocument(); | ||
document.Definitions["Foo"] = schema; | ||
document.Definitions["Bar"] = new JsonSchema4 | ||
{ | ||
Type = JsonObjectType.Object, | ||
Properties = | ||
{ | ||
{ "Foo", new JsonProperty { Reference = schema } } | ||
} | ||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> | ||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\NSwag.Core\NSwag.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,58 @@ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
using NJsonSchema; | ||
using NSwag.Collections; | ||
|
||
namespace NSwag | ||
{ | ||
/// <summary>Container for reusable components (OpenAPI only).</summary> | ||
public class SwaggerComponents | ||
{ | ||
/// <summary></summary> | ||
/// <param name="document"></param> | ||
public SwaggerComponents(SwaggerDocument document) | ||
{ | ||
SecuritySchemes = new Dictionary<string, SwaggerSecurityScheme>(); | ||
|
||
var definitions = new ObservableDictionary<string, JsonSchema4>(); | ||
definitions.CollectionChanged += (sender, args) => | ||
{ | ||
foreach (var path in Schemas.Values) | ||
path.Parent = document; | ||
}; | ||
Schemas = definitions; | ||
|
||
var parameters = new ObservableDictionary<string, SwaggerParameter>(); | ||
parameters.CollectionChanged += (sender, args) => | ||
{ | ||
foreach (var path in Parameters.Values) | ||
path.Parent = document; | ||
}; | ||
Parameters = parameters; | ||
|
||
var responses = new ObservableDictionary<string, SwaggerResponse>(); | ||
responses.CollectionChanged += (sender, args) => | ||
{ | ||
foreach (var path in Responses.Values) | ||
path.Parent = document; | ||
}; | ||
Responses = responses; | ||
} | ||
|
||
/// <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; } | ||
} | ||
} |
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,35 @@ | ||
using NJsonSchema; | ||
using NJsonSchema.Infrastructure; | ||
|
||
namespace NSwag | ||
{ | ||
public partial class SwaggerDocument | ||
{ | ||
/// <summary>Creates the serializer contract resolver based on the <see cref="SchemaType"/>.</summary> | ||
/// <param name="schemaType">The schema type.</param> | ||
/// <returns>The settings.</returns> | ||
public static PropertyRenameAndIgnoreSerializerContractResolver CreateJsonSerializerContractResolver(SchemaType schemaType) | ||
{ | ||
var resolver = JsonSchema4.CreateJsonSerializerContractResolver(schemaType); | ||
|
||
if (schemaType == SchemaType.OpenApi3) | ||
{ | ||
resolver.IgnoreProperty(typeof(SwaggerDocument), "swagger"); | ||
|
||
resolver.IgnoreProperty(typeof(SwaggerDocument), "definitions"); | ||
resolver.IgnoreProperty(typeof(SwaggerDocument), "parameters"); | ||
resolver.IgnoreProperty(typeof(SwaggerDocument), "responses"); | ||
resolver.IgnoreProperty(typeof(SwaggerDocument), "securityDefinitions"); | ||
} | ||
else if (schemaType == SchemaType.Swagger2) | ||
{ | ||
resolver.IgnoreProperty(typeof(SwaggerDocument), "openapi"); | ||
resolver.IgnoreProperty(typeof(SwaggerParameter), "title"); | ||
|
||
resolver.IgnoreProperty(typeof(SwaggerDocument), "components"); | ||
} | ||
|
||
return resolver; | ||
} | ||
} | ||
} |
Oops, something went wrong.