Skip to content

Commit 9ed4887

Browse files
committed
Clean up code and tests
1 parent 1ac5b1b commit 9ed4887

21 files changed

+54
-113
lines changed

src/Microsoft.OpenApi.Readers/V3/JsonSchemaDeserializer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using System.Collections.Generic;
65
using System.Globalization;
76
using System.Text.Json.Nodes;
87
using Json.Schema;
98
using Json.Schema.OpenApi;
10-
using Microsoft.OpenApi.Any;
119
using Microsoft.OpenApi.Interfaces;
1210
using Microsoft.OpenApi.Models;
1311
using Microsoft.OpenApi.Extensions;

src/Microsoft.OpenApi/Extensions/JsonSchemaBuilderExtensions.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ namespace Microsoft.OpenApi.Extensions
1515
/// </summary>
1616
public static class JsonSchemaBuilderExtensions
1717
{
18-
private static readonly Dictionary<string, IJsonSchemaKeyword> _keywords = new Dictionary<string, IJsonSchemaKeyword>();
19-
2018
/// <summary>
2119
/// Custom extensions in the schema
2220
/// </summary>
@@ -114,25 +112,6 @@ public static JsonSchemaBuilder OpenApiExternalDocs(this JsonSchemaBuilder build
114112
return builder;
115113
}
116114

117-
/// <summary>
118-
/// Removes a keyword from the builder instance
119-
/// </summary>
120-
/// <param name="builder"></param>
121-
/// <param name="keyWord"></param>
122-
/// <returns></returns>
123-
public static JsonSchemaBuilder RemoveKeyWord(this JsonSchemaBuilder builder, IJsonSchemaKeyword keyWord)
124-
{
125-
var schema = builder.Build();
126-
var newKeyWords = new List<IJsonSchemaKeyword>();
127-
newKeyWords = schema.Keywords.Where(x => !x.Equals(keyWord)).ToList();
128-
foreach (var item in newKeyWords)
129-
{
130-
builder.Add(item);
131-
}
132-
133-
return builder;
134-
}
135-
136115
/// <summary>
137116
/// Removes a keyword
138117
/// </summary>
@@ -155,7 +134,6 @@ public static JsonSchemaBuilder Remove(this JsonSchemaBuilder builder, string ke
155134
}
156135
}
157136

158-
//_keywords.Remove(keyword);
159137
return schemaBuilder;
160138
}
161139
}

src/Microsoft.OpenApi/Extensions/JsonSchemaExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ public static string GetSummary(this JsonSchema schema)
8484
public static IDictionary<string, IOpenApiExtension> GetExtensions(this JsonSchema schema)
8585
{
8686
return schema.TryGetKeyword<ExtensionsKeyword>(ExtensionsKeyword.Name, out var k) ? k.Extensions! : null;
87-
}
87+
}
8888
}
8989
}

src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -189,7 +189,7 @@ public override void Visit(IDictionary<string, OpenApiLink> links)
189189
{
190190
ResolveMap(links);
191191
}
192-
192+
193193
/// <summary>
194194
/// Resolve all references used in a schem
195195
/// </summary>
@@ -200,17 +200,17 @@ public override void Visit(ref JsonSchema schema)
200200
var description = schema.GetDescription();
201201
var summary = schema.GetSummary();
202202

203-
if (reference != null)
203+
if (schema.Keywords.Count.Equals(1) && reference != null)
204204
{
205205
schema = ResolveJsonSchemaReference(reference, description, summary);
206206
}
207-
207+
208208
var builder = new JsonSchemaBuilder();
209-
foreach (var keyword in schema.Keywords)
209+
foreach (var keyword in schema?.Keywords)
210210
{
211211
builder.Add(keyword);
212212
}
213-
213+
214214
ResolveJsonSchema(schema.GetItems(), r => builder.Items(r));
215215
ResolveJsonSchemaList((IList<JsonSchema>)schema.GetOneOf(), r => builder.OneOf(r));
216216
ResolveJsonSchemaList((IList<JsonSchema>)schema.GetAllOf(), r => builder.AllOf(r));

src/Microsoft.OpenApi/Validations/ValidationRule.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
5-
using System.Globalization;
65
using Microsoft.OpenApi.Interfaces;
76
using Microsoft.OpenApi.Properties;
87

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,24 +186,6 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputName()
186186
Assert.NotEmpty(output);
187187
}
188188

189-
[Fact]
190-
public async Task TransformCommandConvertsCsdlWithDefaultOutputName()
191-
{
192-
var options = new HidiOptions
193-
{
194-
Csdl = Path.Combine("UtilityFiles", "Todo.xml"),
195-
CleanOutput = true,
196-
TerseOutput = false,
197-
InlineLocal = false,
198-
InlineExternal = false,
199-
};
200-
// create a dummy ILogger instance for testing
201-
await OpenApiService.TransformOpenApiDocument(options, _logger);
202-
203-
var output = await File.ReadAllTextAsync("output.yml");
204-
Assert.NotEmpty(output);
205-
}
206-
207189
[Fact]
208190
public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAndSwitchFormat()
209191
{

test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
@@ -159,6 +159,7 @@ public void LoadResponseAndSchemaReference()
159159
["application/json"] = new()
160160
{
161161
Schema = new JsonSchemaBuilder()
162+
.Ref("#/definitions/SampleObject2")
162163
.Description("Sample description")
163164
.Required("name")
164165
.Properties(

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@ public void ShouldParseProducesInAnyOrder()
2222
var reader = new OpenApiStreamReader();
2323
var doc = reader.Read(stream, out var diagnostic);
2424

25-
var successSchema = new JsonSchemaBuilder()
26-
.Type(SchemaValueType.Array)
27-
.Items(new JsonSchemaBuilder()
28-
.Ref("#/definitions/Item"));
29-
3025
var okSchema = new JsonSchemaBuilder()
26+
.Ref("#/definitions/Item")
3127
.Properties(("id", new JsonSchemaBuilder().Type(SchemaValueType.String).Description("Item identifier.")));
3228

3329
var errorSchema = new JsonSchemaBuilder()
30+
.Ref("#/definitions/Error")
3431
.Properties(("code", new JsonSchemaBuilder().Type(SchemaValueType.Integer).Format("int32")),
3532
("message", new JsonSchemaBuilder().Type(SchemaValueType.String)),
3633
("fields", new JsonSchemaBuilder().Type(SchemaValueType.String)));
@@ -165,10 +162,12 @@ public void ShouldAssignSchemaToAllResponses()
165162
var successSchema = new JsonSchemaBuilder()
166163
.Type(SchemaValueType.Array)
167164
.Items(new JsonSchemaBuilder()
165+
.Ref("#/definitions/Item")
168166
.Properties(("id", new JsonSchemaBuilder().Type(SchemaValueType.String).Description("Item identifier."))))
169167
.Build();
170168

171169
var errorSchema = new JsonSchemaBuilder()
170+
.Ref("#/definitions/Error")
172171
.Properties(("code", new JsonSchemaBuilder().Type(SchemaValueType.Integer).Format("int32")),
173172
("message", new JsonSchemaBuilder().Type(SchemaValueType.String)),
174173
("fields", new JsonSchemaBuilder().Type(SchemaValueType.String)))
@@ -201,7 +200,7 @@ public void ShouldAllowComponentsThatJustContainAReference()
201200
JsonSchema schema = doc.Components.Schemas["AllPets"];
202201

203202
// Assert
204-
if (schema.GetRef() != null)
203+
if (schema.Keywords.Count.Equals(1) && schema.GetRef() != null)
205204
{
206205
// detected a cycle - this code gets triggered
207206
Assert.Fail("A cycle should not be detected");

test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/docWithExample.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@ paths: # The available paths and operations for the API
6464
example: File uploaded successfully
6565
components: # Reusable components for the API
6666
schemas: # JSON Schema definitions for the API
67-
User: # A schema for a user object
68-
$id: http://example.com/schemas/user # The identifier for the schema
69-
type: object
70-
properties:
71-
name: # A property for the user name
72-
type: string
73-
default: "John Doe" # The default value for the user name
74-
age: # A property for the user age
75-
type: integer
76-
minimum: 0
77-
default: 18 # The default value for the user age
78-
unevaluatedProperties: false # No additional properties are allowed
7967
Pet: # A schema for a pet object
8068
type: object
8169
required:

test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/documentWithSummaryAndDescriptionInReference.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ paths:
1313
application/json:
1414
schema:
1515
"$ref": '#/components/schemas/pet'
16-
summary: A pet
17-
description: A pet in a petstore
1816
components:
1917
headers:
2018
X-Test:
2119
description: Test
20+
summary: An X-Test header
2221
schema:
2322
type: string
2423
responses:
2524
Test:
26-
description: Test Repsonse
25+
description: Test Response
2726
headers:
2827
X-Test:
2928
$ref: '#/components/headers/X-Test'

0 commit comments

Comments
 (0)