Skip to content

Commit 5015239

Browse files
authored
Merge pull request #1626 from microsoft/mk/validate-json-schema
2 parents efa812a + 6cd0f02 commit 5015239

File tree

100 files changed

+864
-1046
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+864
-1046
lines changed

src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ private static JsonSchema CopySchema(JsonSchema schema, JsonSchema newSchema)
348348
{
349349
schemaBuilder.MinProperties(minProperties);
350350
}
351-
if (schema.GetDiscriminator() == null && newSchema.GetOpenApiDiscriminator() is { } discriminator)
351+
if (schema.GetDiscriminator() == null && newSchema.GetDiscriminator() is { } discriminator)
352352
{
353-
schemaBuilder.Discriminator(discriminator);
353+
schemaBuilder.Discriminator(discriminator.PropertyName, discriminator.Mapping, discriminator.Extensions);
354354
}
355355
if (schema.GetOpenApiExternalDocs() == null && newSchema.GetOpenApiExternalDocs() is { } externalDocs)
356356
{

src/Microsoft.OpenApi/Extensions/JsonSchemaBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ public void Evaluate(EvaluationContext context)
206206
/// The nullable keyword
207207
/// </summary>
208208
[SchemaKeyword(Name)]
209+
[SchemaSpecVersion(SpecVersion.Draft202012)]
209210
public class NullableKeyword : IJsonSchemaKeyword
210211
{
211212
/// <summary>

src/Microsoft.OpenApi/Extensions/OpenApiElementExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public static class OpenApiElementExtensions
2424
public static IEnumerable<OpenApiError> Validate(this IOpenApiElement element, ValidationRuleSet ruleSet)
2525
{
2626
var validator = new OpenApiValidator(ruleSet);
27+
28+
if (element is OpenApiDocument doc)
29+
{
30+
validator.HostDocument = doc;
31+
}
32+
2733
var walker = new OpenApiWalker(validator);
2834
walker.Walk(element);
2935
return validator.Errors.Cast<OpenApiError>().Union(validator.Warnings);

src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public static Type MapJsonSchemaValueTypeToSimpleType(this JsonSchema schema)
130130
return result;
131131
}
132132

133+
/// <summary>
134+
/// Converts the Schema value type to its string equivalent
135+
/// </summary>
136+
/// <param name="value"></param>
137+
/// <returns></returns>
138+
/// <exception cref="NotSupportedException"></exception>
133139
internal static string ConvertSchemaValueTypeToString(SchemaValueType value)
134140
{
135141
return value switch

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,8 @@ public static ReadResult Parse(string input,
676676
/// <exception cref="NotImplementedException"></exception>
677677
public JsonSchema FindSubschema(Json.Pointer.JsonPointer pointer, EvaluationOptions options)
678678
{
679-
throw new NotImplementedException();
679+
var locationUri = string.Concat(BaseUri, pointer);
680+
return (JsonSchema)Workspace.ResolveReference<IBaseDocument>(locationUri);
680681
}
681682
}
682683

src/Microsoft.OpenApi/Reader/ParseNodes/FixedFieldMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.OpenApi.Models;
67

78
namespace Microsoft.OpenApi.Reader.ParseNodes
89
{
9-
internal class FixedFieldMap<T> : Dictionary<string, Action<T, ParseNode>>
10+
internal class FixedFieldMap<T> : Dictionary<string, Action<T, ParseNode, OpenApiDocument>>
1011
{
1112
}
1213
}

src/Microsoft.OpenApi/Reader/ParseNodes/ListNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public ListNode(ParsingContext context, JsonArray jsonArray) : base(
2222
_nodeList = jsonArray;
2323
}
2424

25-
public override List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map)
25+
public override List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
2626
{
2727
if (_nodeList == null)
2828
{
2929
throw new OpenApiReaderException($"Expected list while parsing {typeof(T).Name}", _nodeList);
3030
}
3131

32-
return _nodeList?.Select(n => map(new MapNode(Context, n as JsonObject), null))
32+
return _nodeList?.Select(n => map(new MapNode(Context, n as JsonObject), hostDocument))
3333
.Where(i => i != null)
3434
.ToList();
3535
}

src/Microsoft.OpenApi/Reader/ParseNodes/MapNode.cs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public PropertyNode this[string key]
4949
}
5050
}
5151

52-
public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map)
52+
public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
5353
{
5454
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(T).Name}", Context);
5555
var nodes = jsonMap.Select(
@@ -62,7 +62,7 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument
6262
{
6363
Context.StartObject(key);
6464
value = n.Value is JsonObject jsonObject
65-
? map(new MapNode(Context, jsonObject), null)
65+
? map(new MapNode(Context, jsonObject), hostDocument)
6666
: default;
6767
}
6868
finally
@@ -79,10 +79,11 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument
7979
return nodes.ToDictionary(k => k.key, v => v.value);
8080
}
8181

82-
public override Dictionary<string, JsonSchema> CreateJsonSchemaMapWithReference(
82+
public override Dictionary<string, JsonSchema> CreateJsonSchemaMap(
8383
ReferenceType referenceType,
8484
Func<MapNode, OpenApiDocument, JsonSchema> map,
85-
OpenApiSpecVersion version)
85+
OpenApiSpecVersion version,
86+
OpenApiDocument hostDocument = null)
8687
{
8788
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(JsonSchema).Name}", Context);
8889

@@ -95,30 +96,12 @@ public override Dictionary<string, JsonSchema> CreateJsonSchemaMapWithReference(
9596
{
9697
Context.StartObject(key);
9798
entry = (key,
98-
value: map(new MapNode(Context, (JsonObject)n.Value), null)
99+
value: map(new MapNode(Context, (JsonObject)n.Value), hostDocument)
99100
);
100101
if (entry.value == null)
101102
{
102103
return default; // Body Parameters shouldn't be converted to Parameters
103104
}
104-
// If the component isn't a reference to another component, then point it to itself.
105-
if (entry.value.GetRef() == null)
106-
{
107-
var builder = new JsonSchemaBuilder();
108-
109-
// construct the Ref and append it to the builder
110-
var reference = version == OpenApiSpecVersion.OpenApi2_0 ? string.Concat("#/definitions/", entry.key) :
111-
string.Concat("#/components/schemas/", entry.key);
112-
113-
builder.Ref(reference);
114-
115-
// Append all the keywords in original schema to our new schema using a builder instance
116-
foreach (var keyword in entry.value.Keywords)
117-
{
118-
builder.Add(keyword);
119-
}
120-
entry.value = builder.Build();
121-
}
122105
}
123106
finally
124107
{

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,21 @@ public static ParseNode Create(ParsingContext context, JsonNode node)
4949
return new ValueNode(context, node as JsonValue);
5050
}
5151

52-
public virtual List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map)
52+
public virtual List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
5353
{
5454
throw new OpenApiReaderException("Cannot create list from this type of node.", Context);
5555
}
5656

57-
public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map)
57+
public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
5858
{
5959
throw new OpenApiReaderException("Cannot create map from this type of node.", Context);
6060
}
6161

62-
public virtual Dictionary<string, JsonSchema> CreateJsonSchemaMapWithReference(
62+
public virtual Dictionary<string, JsonSchema> CreateJsonSchemaMap(
6363
ReferenceType referenceType,
6464
Func<MapNode, OpenApiDocument, JsonSchema> map,
65-
OpenApiSpecVersion version)
65+
OpenApiSpecVersion version,
66+
OpenApiDocument hostDocument = null)
6667
{
6768
throw new OpenApiReaderException("Cannot create map from this reference.", Context);
6869
}

src/Microsoft.OpenApi/Reader/ParseNodes/PatternFieldMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.OpenApi.Models;
67

78
namespace Microsoft.OpenApi.Reader.ParseNodes
89
{
9-
internal class PatternFieldMap<T> : Dictionary<Func<string, bool>, Action<T, string, ParseNode>>
10+
internal class PatternFieldMap<T> : Dictionary<Func<string, bool>, Action<T, string, ParseNode, OpenApiDocument>>
1011
{
1112
}
1213
}

0 commit comments

Comments
 (0)