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
6 changes: 6 additions & 0 deletions src/Microsoft.OpenApi.Readers/OpenApiDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Object containing all diagnostic information related to Open API parsing.
/// </summary>
public class OpenApiDiagnostic : IDiagnostic
{
/// <summary>
/// List of all errors.
/// </summary>
public IList<OpenApiError> Errors { get; set; } = new List<OpenApiError>();
}
}
27 changes: 20 additions & 7 deletions src/Microsoft.OpenApi.Readers/OpenApiError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,39 @@

namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Error related to reading Open API YAML/JSON.
/// </summary>
public class OpenApiError
{
private readonly string message;
private readonly string pointer;
private readonly string _message;
private readonly string _pointer;

/// <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 : "");
}
}
}
16 changes: 13 additions & 3 deletions src/Microsoft.OpenApi.Readers/ParseNodes/RootNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,31 @@ public MapNode GetMap()
}
}

/// <summary>
/// Extensions for JSON pointers.
/// </summary>
public static class JsonPointerExtensions
{
public static YamlNode Find(this JsonPointer currentpointer, YamlNode sample)
/// <summary>
/// Finds the YAML node that corresponds to this JSON pointer based on the base YAML node.
/// </summary>
/// <param name="currentpointer"></param>
/// <param name="baseYamlNode"></param>
/// <returns></returns>
public static YamlNode Find(this JsonPointer currentpointer, YamlNode baseYamlNode)
{
if (currentpointer.Tokens.Length == 0)
{
return sample;
return baseYamlNode;
}

try
{
var pointer = sample;
var pointer = baseYamlNode;
foreach (var token in currentpointer.Tokens)
{
var sequence = pointer as YamlSequenceNode;

if (sequence != null)
{
pointer = sequence.Children[Convert.ToInt32(token)];
Expand Down
41 changes: 36 additions & 5 deletions src/Microsoft.OpenApi.Readers/ParsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Parsing context.
/// </summary>
public class ParsingContext
{
private readonly Stack<string> currentLocation = new Stack<string>();
Expand All @@ -23,24 +26,40 @@ public class ParsingContext

private readonly Dictionary<string, object> tempStorage = new Dictionary<string, object>();

public string Version { get; set; }

/// <summary>
/// End the current object.
/// </summary>
public void EndObject()
{
currentLocation.Pop();
}

/// <summary>
/// Get the current location as string representing JSON pointer.
/// </summary>
public string GetLocation()
{
return "#/" + string.Join("/", currentLocation.Reverse().ToArray());
}

/// <summary>
/// Get the referenced object.
/// </summary>
/// <param name="diagnostic"></param>
/// <param name="pointer"></param>
/// <returns></returns>
public IOpenApiReference GetReferencedObject(OpenApiDiagnostic diagnostic, string pointer)
{
var reference = _referenceService.FromString(pointer);
return GetReferencedObject(diagnostic, reference);
}

/// <summary>
///
/// </summary>
/// <param name="diagnostic"></param>
/// <param name="reference"></param>
/// <returns></returns>
public IOpenApiReference GetReferencedObject(OpenApiDiagnostic diagnostic, OpenApiReference reference)
{
IOpenApiReference returnValue = null;
Expand Down Expand Up @@ -72,27 +91,39 @@ public IOpenApiReference GetReferencedObject(OpenApiDiagnostic diagnostic, OpenA
return returnValue;
}

public T GetTempStorage<T>(string key) where T : class
/// <summary>
/// Gets the value from the temporary storage matching the given key.
/// </summary>
public T GetFromTempStorage<T>(string key) where T : class
{
object value;
if (tempStorage.TryGetValue(key, out value))
if (tempStorage.TryGetValue(key, out var value))
{
return (T)value;
}

return null;
}

/// <summary>
/// Sets the reference service.
/// </summary>
/// <param name="referenceService"></param>
public void SetReferenceService(IOpenApiReferenceService referenceService)
{
this._referenceService = referenceService;
}

/// <summary>
/// Sets the temporary storge for this key and value.
/// </summary>
public void SetTempStorage(string key, object value)
{
tempStorage[key] = value;
}

/// <summary>
/// Starts an object with the given object name.
/// </summary>
public void StartObject(string objectName)
{
currentLocation.Push(objectName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ internal static partial class OpenApiV2Deserializer

private static void MakeServers(IList<OpenApiServer> servers, ParsingContext context)
{
var host = context.GetTempStorage<string>("host");
var basePath = context.GetTempStorage<string>("basePath");
var schemes = context.GetTempStorage<List<string>>("schemes");
var host = context.GetFromTempStorage<string>("host");
var basePath = context.GetFromTempStorage<string>("basePath");
var schemes = context.GetFromTempStorage<List<string>>("schemes");

if (schemes != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static OpenApiHeader LoadHeader(ParseNode node)
property.ParseField(header, HeaderFixedFields, HeaderPatternFields);
}

var schema = node.Context.GetTempStorage<OpenApiSchema>("schema");
var schema = node.Context.GetFromTempStorage<OpenApiSchema>("schema");
if (schema != null)
{
header.Schema = schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ internal static OpenApiOperation LoadOperation(ParseNode node)
ParseMap(mapNode, operation, OperationFixedFields, OperationPatternFields);

// Build request body based on information determined while parsing OpenApiOperation
var bodyParameter = node.Context.GetTempStorage<OpenApiParameter>("bodyParameter");
var bodyParameter = node.Context.GetFromTempStorage<OpenApiParameter>("bodyParameter");
if (bodyParameter != null)
{
operation.RequestBody = CreateRequestBody(node.Context, bodyParameter);
}
else
{
var formParameters = node.Context.GetTempStorage<List<OpenApiParameter>>("formParameters");
var formParameters = node.Context.GetFromTempStorage<List<OpenApiParameter>>("formParameters");
if (formParameters != null)
{
operation.RequestBody = CreateFormBody(formParameters);
Expand Down Expand Up @@ -162,8 +162,8 @@ private static OpenApiRequestBody CreateFormBody(List<OpenApiParameter> formPara

private static OpenApiRequestBody CreateRequestBody(ParsingContext context, OpenApiParameter bodyParameter)
{
var consumes = context.GetTempStorage<List<string>>("operationproduces") ??
context.GetTempStorage<List<string>>("globalproduces") ?? new List<string> {"application/json"};
var consumes = context.GetFromTempStorage<List<string>>("operationproduces") ??
context.GetFromTempStorage<List<string>>("globalproduces") ?? new List<string> {"application/json"};

var requestBody = new OpenApiRequestBody
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private static void ProcessIn(OpenApiParameter o, ParseNode n)
n.Context.SetTempStorage("bodyParameter", o);
break;
case "form":
var formParameters = n.Context.GetTempStorage<List<OpenApiParameter>>("formParameters");
var formParameters = n.Context.GetFromTempStorage<List<OpenApiParameter>>("formParameters");
if (formParameters == null)
{
formParameters = new List<OpenApiParameter>();
Expand Down Expand Up @@ -227,7 +227,7 @@ public static OpenApiParameter LoadParameter(ParseNode node)

ParseMap(mapNode, parameter, ParameterFixedFields, ParameterPatternFields);

var schema = node.Context.GetTempStorage<OpenApiSchema>("schema");
var schema = node.Context.GetFromTempStorage<OpenApiSchema>("schema");
if (schema != null)
{
parameter.Schema = schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public static OpenApiResponse LoadResponse(ParseNode node)

private static void ProcessProduces(OpenApiResponse response, ParsingContext context)
{
var produces = context.GetTempStorage<List<string>>("operationproduces") ??
context.GetTempStorage<List<string>>("globalproduces") ?? new List<string> {"application/json"};
var produces = context.GetFromTempStorage<List<string>>("operationproduces") ??
context.GetFromTempStorage<List<string>>("globalproduces") ?? new List<string> {"application/json"};

response.Content = new Dictionary<string, OpenApiMediaType>();
foreach (var mt in produces)
{
var schema = context.GetTempStorage<OpenApiSchema>("operationschema");
var schema = context.GetFromTempStorage<OpenApiSchema>("operationschema");
OpenApiMediaType mediaType = null;
if (schema != null)
{
Expand Down
36 changes: 36 additions & 0 deletions src/Microsoft.OpenApi/Any/AnyType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using Microsoft.OpenApi.Any;

namespace Microsoft.OpenApi.Any
{

/// <summary>
/// Type of an <see cref="IOpenApiAny"/>
/// </summary>
public enum AnyType
{
/// <summary>
/// Primitive.
/// </summary>
Primitive,

/// <summary>
/// Null.
/// </summary>
Null,

/// <summary>
/// Array.
/// </summary>
Array,

/// <summary>
/// Object.
/// </summary>
Object
}
}
34 changes: 4 additions & 30 deletions src/Microsoft.OpenApi/Any/IOpenApiAny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,13 @@
namespace Microsoft.OpenApi.Any
{
/// <summary>
/// Any type kind.
/// </summary>
public enum AnyTypeKind
{
/// <summary>
/// Primitive.
/// </summary>
Primitive,

/// <summary>
/// Null.
/// </summary>
Null,

/// <summary>
/// Array.
/// </summary>
Array,

/// <summary>
/// Object.
/// </summary>
Object
}

/// <summary>
/// Base interface for the Open Api Any.
/// Base interface for all the types that represent Open API Any.
/// </summary>
public interface IOpenApiAny : IOpenApiElement
{
/// <summary>
/// Any type kind.
/// Type of an <see cref="IOpenApiAny"/>.
/// </summary>
AnyTypeKind AnyKind { get; }
AnyType AnyType { get; }
}
}
}
8 changes: 7 additions & 1 deletion src/Microsoft.OpenApi/Any/OpenApiArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@

namespace Microsoft.OpenApi.Any
{
/// <summary>
/// Open API array.
/// </summary>
public class OpenApiArray : List<IOpenApiAny>, IOpenApiAny
{
public AnyTypeKind AnyKind { get; } = AnyTypeKind.Array;
/// <summary>
/// The type of <see cref="IOpenApiAny"/>
/// </summary>
public AnyType AnyType { get; } = AnyType.Array;
}
}
Loading