Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<data name="InputMustBeAFile" xml:space="preserve">
<value>Input parameter '{0}' must be a valid file path.</value>
</data>
<data name="InputMustBeAFileOrUrl" xml:space="preserve">
<value>Input parameter '{0}' must be a valid file path or url.</value>
</data>
<data name="AllowedTopLevelProperties" xml:space="preserve">
<value>Top level properties should be one of name, type, id, location, properties, tags, plan, sku, etag, managedBy, identity. Model definition '{0}' has extra properties ['{1}'].</value>
</data>
Expand Down
36 changes: 28 additions & 8 deletions openapi-diff/src/core/OpenApiDiff.Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class Settings
[SettingsInfo("The location of the old specification.", true)]
[SettingsAlias("o")]
[SettingsAlias("old")]
public string OldSpec { get; set; }
public Uri OldSpec { get; set; }

/// <summary>
/// Gets or sets the path to the new specification file.
/// </summary>
[SettingsInfo("The location of the new specification.", true)]
[SettingsAlias("n")]
[SettingsAlias("new")]
public string NewSpec { get; set; }
public Uri NewSpec { get; set; }

/// <summary>
/// If set to true, print out help message.
Expand Down Expand Up @@ -185,6 +185,10 @@ private static void PopulateSettings(object entityToPopulate, IDictionary<string
{
property.SetValue(entityToPopulate, true);
}
else if (property.PropertyType == typeof(Uri))
{
property.SetValue(entityToPopulate, new Uri(setting.Value.ToString()));
}
else if (property.PropertyType.GetTypeInfo().IsEnum)
{
property.SetValue(entityToPopulate, Enum.Parse(property.PropertyType, setting.Value.ToString(), true));
Expand Down Expand Up @@ -240,21 +244,37 @@ public void Validate()
var doc = property.GetCustomAttributes<SettingsInfoAttribute>().FirstOrDefault();
if (doc != null && doc.IsRequired && property.GetValue(this) == null)
{
Console.WriteLine(String.Format(Resources.ParameterValueIsMissing, property.Name));
Console.WriteLine(string.Format(Resources.ParameterValueIsMissing, property.Name));
throw new Exception(string.Format(Resources.ParameterValueIsMissing, property.Name));
}
}

// Validate input Files
if (!File.Exists(Instance.OldSpec))
if (!ValidateUri(Instance.OldSpec))
{
throw new Exception(String.Format(Resources.InputMustBeAFile, "-old"));
throw new Exception(string.Format(Resources.InputMustBeAFileOrUrl, "-old"));
}

if (!File.Exists(Instance.NewSpec))
if (!ValidateUri(Instance.NewSpec))
{
throw new Exception(String.Format(Resources.InputMustBeAFile, "-new"));
throw new Exception(string.Format(Resources.InputMustBeAFileOrUrl, "-new"));
}
}

private bool ValidateUri(Uri uri)
{
if (Instance.OldSpec.Scheme == Uri.UriSchemeFile &&

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why arent you comparing the parameter and instead the Instance.OldSpec

File.Exists(uri.AbsolutePath))
{
return true;
}

if (Instance.OldSpec.Scheme == Uri.UriSchemeHttp || Instance.OldSpec.Scheme == Uri.UriSchemeHttps)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

{
// todo: validate the uri

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return true;
}

return false;
}
}
}
30 changes: 26 additions & 4 deletions openapi-diff/src/core/OpenApiDiff/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
using OpenApiDiff.Core;
using OpenApiDiff.Properties;
using System.IO;
using System.Net.Http;

namespace OpenApiDiff
{
internal class Program
{
private static int Main(string[] args)
{
Settings settings = Settings.GetInstance(args);
var settings = Settings.GetInstance(args);

if (settings.ShowHelp)
{
Expand All @@ -30,10 +31,10 @@ private static int Main(string[] args)
return 1;
}

SwaggerModeler modeler = new SwaggerModeler();
var modeler = new SwaggerModeler();

string swaggerPrev = File.ReadAllText(settings.OldSpec);
string swaggerNew = File.ReadAllText(settings.NewSpec);
var swaggerPrev = GetOpenApiSpec(settings.OldSpec);
var swaggerNew = GetOpenApiSpec(settings.NewSpec);

var messages = modeler.Compare(swaggerPrev, swaggerNew, settings);
foreach (var msg in messages)
Expand All @@ -43,5 +44,26 @@ private static int Main(string[] args)

return 0;
}


private static string GetOpenApiSpec(Uri uri)
{
if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
{
using (var client = new HttpClient())
{
var response = client.GetAsync(uri).Result;
return response.Content.ReadAsStringAsync().Result;
}
}

if (uri.Scheme == Uri.UriSchemeFile)
{
return File.ReadAllText(uri.AbsolutePath);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please provide a Gist where this has been run successfully so we make sure there is no regression

}

// unsupported Uri scheme
throw new NotImplementedException($"Given Uri scheme ({uri.Scheme}) is not supported yet.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Operation()
/// If a parameter is already defined at the Path Item, the
/// new definition will override it, but can never remove it.
/// </summary>
public IList<SwaggerParameter> Parameters { get; set; }
public IList<SwaggerParameter> Parameters { get; set; } = new List<SwaggerParameter>();

/// <summary>
/// The list of possible responses as they are returned from executing this operation.
Expand Down