Skip to content

Commit

Permalink
Added OpenApiServerVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Mar 14, 2018
1 parent 6cabe7f commit 8dfa231
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 29 deletions.
59 changes: 59 additions & 0 deletions src/NSwag.Core.Tests/Serialization/ComponentsSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
using System.Linq;
using System.Threading.Tasks;
using NJsonSchema;
using Xunit;

namespace NSwag.Core.Tests.Serialization
{
public class ServersSerializationTests
{
[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);

//// Assert
Assert.Equal("rsuter.com", document.Host);
Assert.Equal("/myapi", document.BasePath);
Assert.Equal(SwaggerSchema.Http, document.Schemes.First());
Assert.Equal(SwaggerSchema.Https, document.Schemes.Last());

Assert.Contains(@"""basePath""", json);
}

[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);

//// Assert
Assert.Equal(2, document.Servers.Count);
Assert.Contains(@"""servers""", json);
}

private static SwaggerDocument CreateDocument()
{
var schema = new JsonSchema4
{
Type = JsonObjectType.String
};

var document = new SwaggerDocument
{
Servers =
{
new OpenApiServer
{
Url = "http://rsuter.com/myapi"
}
}
};

document.Schemes.Add(SwaggerSchema.Https);

return document;
}
}

public class ComponentsSerializationTests
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;
using NJsonSchema;
using NSwag.Collections;

namespace NSwag
{
/// <summary>Container for reusable components (OpenAPI only).</summary>
public class SwaggerComponents
public class OpenApiComponents
{
/// <summary></summary>
/// <param name="document"></param>
public SwaggerComponents(SwaggerDocument document)
public OpenApiComponents(SwaggerDocument document)
{
SecuritySchemes = new Dictionary<string, SwaggerSecurityScheme>();
Examples = new Dictionary<string, SwaggerExample>();
Expand Down
4 changes: 3 additions & 1 deletion src/NSwag.Core/OpenApiServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
//-----------------------------------------------------------------------

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

namespace NSwag
{
/// <summary>Describes an OpenAPI server.</summary>
public class OpenApiServer
{
/// <summary>Gets or sets the URL of the server.</summary>
Expand All @@ -22,6 +24,6 @@ public class OpenApiServer

/// <summary>Gets or sets the variables of the server.</summary>
[JsonProperty(PropertyName = "variables", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Variables { get; set; }
public IDictionary<string, OpenApiServerVariable> Variables { get; } = new Dictionary<string, OpenApiServerVariable>();
}
}
30 changes: 30 additions & 0 deletions src/NSwag.Core/OpenApiServerVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//-----------------------------------------------------------------------
// <copyright file="OpenApiServerVariable.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, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace NSwag
{
/// <summary>Describes an OpenAPI server variable.</summary>
public class OpenApiServerVariable
{
/// <summary>Gets or sets the URL of the server.</summary>
[JsonProperty(PropertyName = "url", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public ICollection<string> Enum { get; } = new Collection<string>();

/// <summary>Gets or sets the variables of the server.</summary>
[JsonProperty(PropertyName = "default", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Default { get; set; }

/// <summary>Gets or sets the description of the server.</summary>
[JsonProperty(PropertyName = "description", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Description { get; set; }
}
}
14 changes: 10 additions & 4 deletions src/NSwag.Core/SwaggerDocument.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public static PropertyRenameAndIgnoreSerializerContractResolver CreateJsonSerial
if (schemaType == SchemaType.OpenApi3)
{
resolver.IgnoreProperty(typeof(SwaggerDocument), "swagger");

resolver.IgnoreProperty(typeof(SwaggerDocument), "host");
resolver.IgnoreProperty(typeof(SwaggerDocument), "basePath");
resolver.IgnoreProperty(typeof(SwaggerDocument), "schemes");

//resolver.IgnoreProperty(typeof(SwaggerDocument), "consumes");
//resolver.IgnoreProperty(typeof(SwaggerDocument), "produces");

resolver.IgnoreProperty(typeof(SwaggerDocument), "definitions");
resolver.IgnoreProperty(typeof(SwaggerDocument), "parameters");
Expand Down Expand Up @@ -60,7 +66,7 @@ public string BasePath
get
{
var segments = Servers?.FirstOrDefault()?.Url?.Replace("http://", "").Replace("https://", "").Split('/').Skip(1);
return segments != null ? string.Join("/", segments) : null;
return segments != null ? "/" + string.Join("/", segments) : null;
}
set { UpdateServers(Schemes, Host, value); }
}
Expand All @@ -86,19 +92,19 @@ public ICollection<SwaggerSchema> Schemes
_schemes.CollectionChanged += OnSchemesChanged;
return _schemes;
}
set { UpdateServers(value, Host, BaseUrl); }
set { UpdateServers(value, Host, BasePath); }
}

private void OnSchemesChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateServers(Schemes, Host, BaseUrl);
UpdateServers((ICollection<SwaggerSchema>)sender, Host, BasePath);
}

private void UpdateServers(ICollection<SwaggerSchema> schemes, string host, string basePath)
{
Servers = schemes?.Select(s => new OpenApiServer
{
Url = s + host + basePath
Url = s.ToString().ToLowerInvariant() + "://" + host + basePath
}).ToList() ?? new List<OpenApiServer>();
}

Expand Down
22 changes: 3 additions & 19 deletions src/NSwag.Core/SwaggerDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NJsonSchema;
using NJsonSchema.Generation;
using NJsonSchema.Infrastructure;
Expand All @@ -32,9 +30,7 @@ public SwaggerDocument()
OpenApi = "3.0";

Info = new SwaggerInfo();
Schemes = new List<SwaggerSchema>();

Components = new SwaggerComponents(this);
Components = new OpenApiComponents(this);

var paths = new ObservableDictionary<string, SwaggerOperations>();
paths.CollectionChanged += (sender, args) =>
Expand Down Expand Up @@ -84,7 +80,7 @@ public SwaggerDocument()

/// <summary>Gets or sets the components.</summary>
[JsonProperty(PropertyName = "components", DefaultValueHandling = DefaultValueHandling.Ignore)]
public SwaggerComponents Components { get; }
public OpenApiComponents Components { get; }

/// <summary>Gets or sets a security description.</summary>
[JsonProperty(PropertyName = "security", DefaultValueHandling = DefaultValueHandling.Ignore)]
Expand All @@ -96,19 +92,7 @@ public SwaggerDocument()

/// <summary>Gets the base URL of the web service.</summary>
[JsonIgnore]
public string BaseUrl
{
get
{
if (string.IsNullOrEmpty(Host))
return "";

if (Schemes.Any())
return (Schemes.First().ToString().ToLowerInvariant() + "://" + Host + (!string.IsNullOrEmpty(BasePath) ? "/" + BasePath.Trim('/') : string.Empty)).Trim('/');

return ("http://" + Host + (!string.IsNullOrEmpty(BasePath) ? "/" + BasePath.Trim('/') : string.Empty)).Trim('/');
}
}
public string BaseUrl => Servers?.FirstOrDefault()?.Url ?? "";

/// <summary>Gets or sets the external documentation.</summary>
[JsonProperty(PropertyName = "externalDocs", DefaultValueHandling = DefaultValueHandling.Ignore)]
Expand Down
Loading

0 comments on commit 8dfa231

Please sign in to comment.