Skip to content

Commit e25dc2b

Browse files
authored
Merge branch 'master' into MapCustomTypeToString
2 parents f791354 + b792ec3 commit e25dc2b

37 files changed

+188
-204
lines changed

src/Microsoft.OpenApi.Readers/OpenApiDiagnostic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
5+
using Microsoft.OpenApi.Models;
56
using Microsoft.OpenApi.Readers.Interface;
67

78
namespace Microsoft.OpenApi.Readers
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Microsoft.OpenApi.Exceptions;
10+
using Microsoft.OpenApi.Models;
11+
using SharpYaml;
12+
13+
namespace Microsoft.OpenApi.Readers
14+
{
15+
/// <summary>
16+
/// Error detected during the reading of some input and converting to an OpenApiDocument
17+
/// </summary>
18+
public class OpenApiReaderError : OpenApiError
19+
{
20+
21+
/// <summary>
22+
/// Creates error object from thrown exception
23+
/// </summary>
24+
/// <param name="exception"></param>
25+
public OpenApiReaderError(OpenApiException exception) : base(exception)
26+
{
27+
28+
}
29+
30+
/// <summary>
31+
/// Create error object from YAML SyntaxErrorException
32+
/// </summary>
33+
/// <param name="exception"></param>
34+
public OpenApiReaderError(SyntaxErrorException exception) : base(String.Empty, exception.Message)
35+
{
36+
37+
}
38+
}
39+
}

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
5050
}
5151
catch (SyntaxErrorException ex)
5252
{
53-
diagnostic.Errors.Add(new OpenApiError(string.Empty, ex.Message));
53+
diagnostic.Errors.Add(new OpenApiReaderError(ex));
5454
return new OpenApiDocument();
5555
}
5656

@@ -83,7 +83,6 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
8383
case ReferenceResolutionSetting.DoNotResolveReferences:
8484
break;
8585
}
86-
8786
}
8887
catch (OpenApiException ex)
8988
{
@@ -94,7 +93,7 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
9493
var errors = document.Validate(_settings.RuleSet);
9594
foreach (var item in errors)
9695
{
97-
diagnostic.Errors.Add(new OpenApiError(item.ErrorPath, item.ErrorMessage));
96+
diagnostic.Errors.Add(item);
9897
}
9998

10099
return document;

src/Microsoft.OpenApi.Readers/ParseNodes/ParseNode.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ protected ParseNode(ParsingContext parsingContext, OpenApiDiagnostic diagnostic)
2424

2525
public OpenApiDiagnostic Diagnostic { get; }
2626

27-
public string DomainType { get; internal set; }
28-
2927
public MapNode CheckMapNode(string nodeName)
3028
{
3129
var mapNode = this as MapNode;
@@ -37,17 +35,6 @@ public MapNode CheckMapNode(string nodeName)
3735
return mapNode;
3836
}
3937

40-
internal string CheckRegex(string value, Regex versionRegex, string defaultValue)
41-
{
42-
if (!versionRegex.IsMatch(value))
43-
{
44-
Diagnostic.Errors.Add(new OpenApiError("", "Value does not match regex: " + versionRegex));
45-
return defaultValue;
46-
}
47-
48-
return value;
49-
}
50-
5138
public static ParseNode Create(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node)
5239
{
5340
var listNode = node as YamlSequenceNode;

src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using Microsoft.OpenApi.Any;
88
using Microsoft.OpenApi.Exceptions;
9+
using Microsoft.OpenApi.Models;
910
using SharpYaml.Serialization;
1011

1112
namespace Microsoft.OpenApi.Readers.ParseNodes
@@ -42,7 +43,7 @@ public void ParseField<T>(
4243
catch (OpenApiException ex)
4344
{
4445
ex.Pointer = Context.GetLocation();
45-
Diagnostic.Errors.Add(new OpenApiError(ex));
46+
Diagnostic.Errors.Add(new OpenApiReaderError(ex));
4647
}
4748
finally
4849
{
@@ -62,7 +63,7 @@ public void ParseField<T>(
6263
catch (OpenApiException ex)
6364
{
6465
ex.Pointer = Context.GetLocation();
65-
Diagnostic.Errors.Add(new OpenApiError(ex));
66+
Diagnostic.Errors.Add(new OpenApiReaderError(ex));
6667
}
6768
finally
6869
{

src/Microsoft.OpenApi/Extensions/OpenApiElementExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using Microsoft.OpenApi.Interfaces;
6+
using Microsoft.OpenApi.Models;
67
using Microsoft.OpenApi.Services;
78
using Microsoft.OpenApi.Validations;
89

@@ -19,7 +20,8 @@ public static class OpenApiElementExtensions
1920
/// <param name="element">Element to validate</param>
2021
/// <param name="ruleSet">Optional set of rules to use for validation</param>
2122
/// <returns>An IEnumerable of errors. This function will never return null.</returns>
22-
public static IEnumerable<ValidationError> Validate(this IOpenApiElement element, ValidationRuleSet ruleSet = null) {
23+
public static IEnumerable<OpenApiError> Validate(this IOpenApiElement element, ValidationRuleSet ruleSet = null)
24+
{
2325
var validator = new OpenApiValidator(ruleSet);
2426
var walker = new OpenApiWalker(validator);
2527
walker.Walk(element);

src/Microsoft.OpenApi.Readers/OpenApiError.cs renamed to src/Microsoft.OpenApi/Models/OpenApiError.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33

44
using Microsoft.OpenApi.Exceptions;
55

6-
namespace Microsoft.OpenApi.Readers
6+
namespace Microsoft.OpenApi.Models
77
{
88
/// <summary>
9-
/// Error related to reading Open API YAML/JSON.
9+
/// Error related to the Open API Document.
1010
/// </summary>
1111
public class OpenApiError
1212
{
1313
/// <summary>
1414
/// Initializes the <see cref="OpenApiError"/> class using the message and pointer from the given exception.
1515
/// </summary>
16-
public OpenApiError(OpenApiException exception)
16+
public OpenApiError(OpenApiException exception) : this(exception.Pointer, exception.Message)
1717
{
18-
Message = exception.Message;
19-
Pointer = exception.Pointer;
2018
}
2119

2220
/// <summary>

src/Microsoft.OpenApi/Validations/IValidationContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using Microsoft.OpenApi.Models;
78
using Microsoft.OpenApi.Services;
89
using Microsoft.OpenApi.Validations.Rules;
910

@@ -18,7 +19,8 @@ public interface IValidationContext
1819
/// Register an error with the validation context.
1920
/// </summary>
2021
/// <param name="error">Error to register.</param>
21-
void AddError(ValidationError error);
22+
void AddError(OpenApiValidatorError error);
23+
2224

2325
/// <summary>
2426
/// Allow Rule to indicate validation error occured at a deeper context level.

src/Microsoft.OpenApi/Validations/OpenApiValidator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.OpenApi.Validations
1616
public class OpenApiValidator : OpenApiVisitorBase, IValidationContext
1717
{
1818
private readonly ValidationRuleSet _ruleSet;
19-
private readonly IList<ValidationError> _errors = new List<ValidationError>();
19+
private readonly IList<OpenApiValidatorError> _errors = new List<OpenApiValidatorError>();
2020

2121
/// <summary>
2222
/// Create a vistor that will validate an OpenAPIDocument
@@ -30,7 +30,7 @@ public OpenApiValidator(ValidationRuleSet ruleSet = null)
3030
/// <summary>
3131
/// Gets the validation errors.
3232
/// </summary>
33-
public IEnumerable<ValidationError> Errors
33+
public IEnumerable<OpenApiValidatorError> Errors
3434
{
3535
get
3636
{
@@ -42,7 +42,7 @@ public IEnumerable<ValidationError> Errors
4242
/// Register an error with the validation context.
4343
/// </summary>
4444
/// <param name="error">Error to register.</param>
45-
public void AddError(ValidationError error)
45+
public void AddError(OpenApiValidatorError error)
4646
{
4747
if (error == null)
4848
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Microsoft.OpenApi.Models;
10+
11+
namespace Microsoft.OpenApi.Validations
12+
{
13+
/// <summary>
14+
/// Errors detected when validating an OpenAPI Element
15+
/// </summary>
16+
public class OpenApiValidatorError : OpenApiError
17+
{
18+
/// <summary>
19+
/// Initializes the <see cref="OpenApiError"/> class.
20+
/// </summary>
21+
public OpenApiValidatorError(string ruleName, string pointer, string message) : base(pointer, message)
22+
{
23+
RuleName = ruleName;
24+
}
25+
26+
/// <summary>
27+
/// Name of rule that detected the error.
28+
/// </summary>
29+
public string RuleName { get; set; }
30+
}
31+
}

0 commit comments

Comments
 (0)