Skip to content
Merged
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
22 changes: 14 additions & 8 deletions src/Microsoft.OpenApi.Readers/OpenApiError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,40 @@ namespace Microsoft.OpenApi.Readers
/// </summary>
public class OpenApiError
{
private readonly string _message;
private readonly string _pointer;
/// <summary>
/// Message explaining the error.
/// </summary>
public string Message { get; set; }

/// <summary>
/// Pointer to the location of the error.
/// </summary>
public string Pointer { get; set; }

/// <summary>
/// Initializes the <see cref="OpenApiError"/> class using the message and pointer from the given exception.
/// </summary>
public OpenApiError(OpenApiException exception)
{
_message = exception.Message;
_pointer = exception.Pointer;
Message = exception.Message;
Pointer = exception.Pointer;
}

/// <summary>
/// Initializes the <see cref="OpenApiError"/> class.
/// </summary>
public OpenApiError(string pointer, string message)
{
this._pointer = pointer;
this._message = message;
this.Pointer = pointer;
this.Message = message;
}

/// <summary>
/// Gets the string representation of <see cref="OpenApiError"/>.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return _message + (!string.IsNullOrEmpty(_pointer) ? " at " + _pointer : "");
return Message + (!string.IsNullOrEmpty(Pointer) ? " at " + Pointer : "");
}
}
}
8 changes: 6 additions & 2 deletions src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlMapping
{
if (node == null)
{
throw new OpenApiException($"Expected map");
throw new OpenApiException("Expected map");
}

this.node = node;
Expand Down Expand Up @@ -72,8 +72,12 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
n => new
{
key = n.Key.GetScalarValue(),
value = map(new MapNode(Context, Diagnostic, n.Value as YamlMappingNode))
value = n.Value as YamlMappingNode == null ?
default(T) :
map(new MapNode(Context, Diagnostic, n.Value as YamlMappingNode))

});

return nodes.ToDictionary(k => k.key, v => v.value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
Expand All @@ -21,7 +22,7 @@ internal static partial class OpenApiV2Deserializer
{
"swagger", (o, n) =>
{
/* Ignore it */
o.SpecVersion = new Version(2, 0);
}
},
{"info", (o, n) => o.Info = LoadInfo(n)},
Expand All @@ -31,10 +32,7 @@ internal static partial class OpenApiV2Deserializer
"schemes", (o, n) => n.Context.SetTempStorage(
"schemes",
n.CreateSimpleList(
s =>
{
return s.GetScalarValue();
}))
s => s.GetScalarValue()))
},
{
"consumes",
Expand Down
20 changes: 16 additions & 4 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,27 @@ internal static partial class OpenApiV2Deserializer
o.Default = new OpenApiString(n.GetScalarValue());
}
},

// discriminator
{
"discriminator", (o, n) =>
{
o.Discriminator = new OpenApiDiscriminator()
{
PropertyName = n.GetScalarValue()
};
}
},
{
"readOnly", (o, n) =>
{
o.ReadOnly = bool.Parse(n.GetScalarValue());
}
},
// xml
{
"xml", (o, n) =>
{
o.Xml = LoadXml(n);
}
},
{
"externalDocs", (o, n) =>
{
Expand All @@ -181,7 +193,7 @@ internal static partial class OpenApiV2Deserializer
{
"example", (o, n) =>
{
o.Example = new OpenApiString(n.GetScalarValue());
o.Example = n.CreateAny();
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,64 @@ namespace Microsoft.OpenApi.Readers.V2
/// </summary>
internal static partial class OpenApiV2Deserializer
{
private static string flowValue;

private static OpenApiOAuthFlow flow;

private static readonly FixedFieldMap<OpenApiSecurityScheme> SecuritySchemeFixedFields =
new FixedFieldMap<OpenApiSecurityScheme>
{
{
"type",
(o, n) => o.Type = (SecuritySchemeType)Enum.Parse(typeof(SecuritySchemeType), n.GetScalarValue())
(o, n) =>
{
var type = n.GetScalarValue();
switch (type)
{
case "basic":
o.Type = SecuritySchemeType.Http;
o.Scheme = "basic";
break;

case "apiKey":
o.Type = SecuritySchemeType.ApiKey;
break;

case "oauth2":
o.Type = SecuritySchemeType.OAuth2;
break;
}
}
},
{"description", (o, n) => o.Description = n.GetScalarValue()},
{"name", (o, n) => o.Name = n.GetScalarValue()},
{"in", (o, n) => o.In = n.GetScalarValue().GetEnumFromDisplayName<ParameterLocation>()},
{"scheme", (o, n) => o.Scheme = n.GetScalarValue()},
{"bearerFormat", (o, n) => o.BearerFormat = n.GetScalarValue()},
{
"openIdConnectUrl",
(o, n) => o.OpenIdConnectUrl = new Uri(n.GetScalarValue(), UriKind.RelativeOrAbsolute)
"flow", (o, n) =>
{
flowValue = n.GetScalarValue();
}
},
{
"authorizationUrl",
(o, n) =>
{
flow.AuthorizationUrl = new Uri(n.GetScalarValue());
}
},
{"flows", (o, n) => o.Flows = LoadOAuthFlows(n)}
{
"tokenUrl",
(o, n) =>
{
flow.TokenUrl = new Uri(n.GetScalarValue());
}
},
{
"scopes", (o, n) =>
{
flow.Scopes = n.CreateSimpleMap(LoadString);
}
}
};

private static readonly PatternFieldMap<OpenApiSecurityScheme> SecuritySchemePatternFields =
Expand All @@ -43,6 +84,10 @@ internal static partial class OpenApiV2Deserializer

public static OpenApiSecurityScheme LoadSecurityScheme(ParseNode node)
{
// Reset the local variables every time this method is called.
flowValue = null;
flow = new OpenApiOAuthFlow();

var mapNode = node.CheckMapNode("securityScheme");

var securityScheme = new OpenApiSecurityScheme();
Expand All @@ -51,6 +96,36 @@ public static OpenApiSecurityScheme LoadSecurityScheme(ParseNode node)
property.ParseField(securityScheme, SecuritySchemeFixedFields, SecuritySchemePatternFields);
}

// Put the Flow object in the right Flows property based on the string in "flow"
if (flowValue == OpenApiConstants.Implicit)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if [](start = 12, length = 2)

nit, consider a switch statement

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll make this fix with a future PR.

{
securityScheme.Flows = new OpenApiOAuthFlows
{
Implicit = flow
};
}
else if (flowValue == OpenApiConstants.Password)
{
securityScheme.Flows = new OpenApiOAuthFlows
{
Password = flow
};
}
else if (flowValue == OpenApiConstants.Application)
{
securityScheme.Flows = new OpenApiOAuthFlows
{
ClientCredentials = flow
};
}
else if (flowValue == OpenApiConstants.AccessCode)
{
securityScheme.Flows = new OpenApiOAuthFlows
{
AuthorizationCode = flow
};
}

return securityScheme;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Microsoft.OpenApi.Readers/V2/OpenApiV2Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ private static void ParseMap<T>(

private static void ReportMissing(ParseNode node, IList<string> required)
{
foreach (var error in required.Select(r => new OpenApiError("", $"{r} is a required property")).ToList())
foreach (var error in required.Select(
r => new OpenApiError(
node.Context.GetLocation(),
$"{r} is a required property"))
.ToList())
{
node.Diagnostic.Errors.Add(error);
}
Expand Down
73 changes: 73 additions & 0 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiXmlDeserializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using System;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;

namespace Microsoft.OpenApi.Readers.V2
{
/// <summary>
/// Class containing logic to deserialize Open API V3 document into

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will fix comment to V2

/// runtime Open API object model.
/// </summary>
internal static partial class OpenApiV2Deserializer
{
private static readonly FixedFieldMap<OpenApiXml> XmlFixedFields = new FixedFieldMap<OpenApiXml>
{
{
"name", (o, n) =>
{
o.Name = n.GetScalarValue();
}
},
{
"namespace", (o, n) =>
{
o.Namespace = new Uri(n.GetScalarValue());
}
},
{
"prefix", (o, n) =>
{
o.Prefix = n.GetScalarValue();
}
},
{
"attribute", (o, n) =>
{
o.Attribute = bool.Parse(n.GetScalarValue());
}
},
{
"wrapped", (o, n) =>
{
o.Wrapped = bool.Parse(n.GetScalarValue());
}
},
};

private static readonly PatternFieldMap<OpenApiXml> XmlPatternFields =
new PatternFieldMap<OpenApiXml>
{
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
};

public static OpenApiXml LoadXml(ParseNode node)
{
var mapNode = node.CheckMapNode("xml");

var xml = new OpenApiXml();
foreach (var property in mapNode)
{
property.ParseField(xml, XmlFixedFields, XmlPatternFields);
}

return xml;
}
}
}
Loading