Skip to content
This repository was archived by the owner on Feb 1, 2026. It is now read-only.

Commit 93db0d6

Browse files
committed
jsonschemaparser内でunknowntypeが見つかった場合は専用のdiagnosticsを生成するように変更
1 parent fe56757 commit 93db0d6

File tree

3 files changed

+248
-3
lines changed

3 files changed

+248
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using mooresmaster.Generator.Json;
2+
3+
namespace mooresmaster.Generator.Analyze.Diagnostics;
4+
5+
/// <summary>
6+
/// スキーマのtypeが不明な値の場合のDiagnostics
7+
/// </summary>
8+
public class UnknownTypeDiagnostics : IDiagnostics
9+
{
10+
public UnknownTypeDiagnostics(JsonObject json, string? propertyName, string unknownType, Location typeLocation)
11+
{
12+
Json = json;
13+
PropertyName = propertyName;
14+
UnknownType = unknownType;
15+
Locations = new[] { typeLocation };
16+
}
17+
18+
public JsonObject Json { get; }
19+
public string? PropertyName { get; }
20+
public string UnknownType { get; }
21+
public Location[] Locations { get; }
22+
23+
public string Message => PropertyName != null
24+
? $"Property '{PropertyName}' has unknown type '{UnknownType}'. Valid types are: object, array, string, enum, number, integer, boolean, uuid, vector2, vector3, vector4, vector2Int, vector3Int."
25+
: $"Schema has unknown type '{UnknownType}'. Valid types are: object, array, string, enum, number, integer, boolean, uuid, vector2, vector3, vector4, vector2Int, vector3Int.";
26+
}

mooresmaster.Generator/JsonSchema/JsonSchemaParser.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ private static Falliable<SchemaId> Parse(JsonObject root, SchemaId? parent, bool
148148
}
149149

150150
var type = typeString.Literal;
151-
return Falliable<SchemaId>.Success(type switch
151+
152+
SchemaId? schemaId = type switch
152153
{
153154
Tokens.ObjectType => ParseObject(root, parent, isInterfaceProperty, schemaTable, analysis),
154155
Tokens.ArrayType => ParseArray(root, parent, isInterfaceProperty, schemaTable, analysis),
@@ -163,8 +164,17 @@ private static Falliable<SchemaId> Parse(JsonObject root, SchemaId? parent, bool
163164
Tokens.Vector4Type => ParseVector4(root, parent, isInterfaceProperty, schemaTable, analysis),
164165
Tokens.Vector2IntType => ParseVector2Int(root, parent, isInterfaceProperty, schemaTable, analysis),
165166
Tokens.Vector3IntType => ParseVector3Int(root, parent, isInterfaceProperty, schemaTable, analysis),
166-
_ => throw new Exception($"Unknown type: {type}")
167-
});
167+
_ => null
168+
};
169+
170+
if (schemaId == null)
171+
{
172+
var propertyName = (root[Tokens.PropertyNameKey] as JsonString)?.Literal;
173+
analysis.ReportDiagnostics(new UnknownTypeDiagnostics(root, propertyName, type, typeString.Location));
174+
return Falliable<SchemaId>.Failure();
175+
}
176+
177+
return Falliable<SchemaId>.Success(schemaId.Value);
168178
}
169179

170180
private static SchemaId ParseObject(JsonObject json, SchemaId? parent, bool isInterfaceProperty, SchemaTable table, Analysis analysis)
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
using mooresmaster.Generator.Analyze.Diagnostics;
2+
using Xunit;
3+
4+
namespace mooresmaster.Tests.AnalyzerTests;
5+
6+
/// <summary>
7+
/// スキーマのtypeが不明な値の場合のDiagnosticsのテスト
8+
/// </summary>
9+
public class UnknownTypeTest
10+
{
11+
/// <summary>
12+
/// 不明なtypeの場合のエラーのテスト
13+
/// </summary>
14+
[Fact]
15+
public void PropertyWithUnknownTypeTest()
16+
{
17+
const string schema =
18+
"""
19+
id: testSchema
20+
type: object
21+
properties:
22+
- key: myProperty
23+
type: unknownType
24+
""";
25+
26+
var diagnosticsArray = Test.Generate(schema).analysis.DiagnosticsList;
27+
28+
Assert.Single(diagnosticsArray);
29+
var diagnostics = Assert.IsType<UnknownTypeDiagnostics>(diagnosticsArray[0]);
30+
Assert.Equal("myProperty", diagnostics.PropertyName);
31+
Assert.Equal("unknownType", diagnostics.UnknownType);
32+
}
33+
34+
/// <summary>
35+
/// 有効なtypeの場合はエラーが出ないテスト
36+
/// </summary>
37+
[Fact]
38+
public void PropertyWithValidTypeTest()
39+
{
40+
const string schema =
41+
"""
42+
id: testSchema
43+
type: object
44+
properties:
45+
- key: myProperty
46+
type: string
47+
""";
48+
49+
var diagnosticsArray = Test.Generate(schema).analysis.DiagnosticsList;
50+
51+
Assert.Empty(diagnosticsArray);
52+
}
53+
54+
/// <summary>
55+
/// ネストされたオブジェクト内で不明なtypeの場合のエラーのテスト
56+
/// </summary>
57+
[Fact]
58+
public void NestedObjectWithUnknownTypeTest()
59+
{
60+
const string schema =
61+
"""
62+
id: testSchema
63+
type: object
64+
properties:
65+
- key: nested
66+
type: object
67+
properties:
68+
- key: innerProperty
69+
type: invalidType
70+
""";
71+
72+
var diagnosticsArray = Test.Generate(schema).analysis.DiagnosticsList;
73+
74+
Assert.Single(diagnosticsArray);
75+
var diagnostics = Assert.IsType<UnknownTypeDiagnostics>(diagnosticsArray[0]);
76+
Assert.Equal("innerProperty", diagnostics.PropertyName);
77+
Assert.Equal("invalidType", diagnostics.UnknownType);
78+
}
79+
80+
/// <summary>
81+
/// 配列のitemsで不明なtypeの場合のエラーのテスト
82+
/// </summary>
83+
[Fact]
84+
public void ArrayItemsWithUnknownTypeTest()
85+
{
86+
const string schema =
87+
"""
88+
id: testSchema
89+
type: object
90+
properties:
91+
- key: myArray
92+
type: array
93+
items:
94+
type: badType
95+
""";
96+
97+
var diagnosticsArray = Test.Generate(schema).analysis.DiagnosticsList;
98+
99+
Assert.Single(diagnosticsArray);
100+
var diagnostics = Assert.IsType<UnknownTypeDiagnostics>(diagnosticsArray[0]);
101+
Assert.Equal("badType", diagnostics.UnknownType);
102+
}
103+
104+
/// <summary>
105+
/// Locationが正しくセットされていることのテスト
106+
/// </summary>
107+
[Fact]
108+
public void DiagnosticsContainsCorrectLocationTest()
109+
{
110+
const string schema =
111+
"""
112+
id: testSchema
113+
type: object
114+
properties:
115+
- key: myProperty
116+
type: unknownType
117+
""";
118+
119+
var diagnosticsArray = Test.Generate(schema).analysis.DiagnosticsList;
120+
121+
Assert.Single(diagnosticsArray);
122+
var diagnostics = Assert.IsType<UnknownTypeDiagnostics>(diagnosticsArray[0]);
123+
Assert.Single(diagnostics.Locations);
124+
// Locationがtypeの値の位置を指していること(5行目)
125+
Assert.Equal(5, diagnostics.Locations[0].StartLine);
126+
}
127+
128+
/// <summary>
129+
/// メッセージが正しいことのテスト(プロパティ名あり)
130+
/// </summary>
131+
[Fact]
132+
public void DiagnosticsMessageWithPropertyNameTest()
133+
{
134+
const string schema =
135+
"""
136+
id: testSchema
137+
type: object
138+
properties:
139+
- key: myProperty
140+
type: unknownType
141+
""";
142+
143+
var diagnosticsArray = Test.Generate(schema).analysis.DiagnosticsList;
144+
145+
Assert.Single(diagnosticsArray);
146+
var diagnostics = Assert.IsType<UnknownTypeDiagnostics>(diagnosticsArray[0]);
147+
Assert.Contains("myProperty", diagnostics.Message);
148+
Assert.Contains("unknownType", diagnostics.Message);
149+
}
150+
151+
/// <summary>
152+
/// 複数のプロパティで不明なtypeの場合、複数のエラーが出るテスト
153+
/// </summary>
154+
[Fact]
155+
public void MultiplePropertiesWithUnknownTypeTest()
156+
{
157+
const string schema =
158+
"""
159+
id: testSchema
160+
type: object
161+
properties:
162+
- key: property1
163+
type: badType1
164+
- key: property2
165+
type: badType2
166+
- key: property3
167+
type: string
168+
""";
169+
170+
var diagnosticsArray = Test.Generate(schema).analysis.DiagnosticsList;
171+
172+
Assert.Equal(2, diagnosticsArray.Count);
173+
var diag1 = Assert.IsType<UnknownTypeDiagnostics>(diagnosticsArray[0]);
174+
var diag2 = Assert.IsType<UnknownTypeDiagnostics>(diagnosticsArray[1]);
175+
Assert.Equal("property1", diag1.PropertyName);
176+
Assert.Equal("badType1", diag1.UnknownType);
177+
Assert.Equal("property2", diag2.PropertyName);
178+
Assert.Equal("badType2", diag2.UnknownType);
179+
}
180+
181+
/// <summary>
182+
/// 全ての有効なtypeでエラーが出ないテスト
183+
/// </summary>
184+
[Theory]
185+
[InlineData("string")]
186+
[InlineData("integer")]
187+
[InlineData("number")]
188+
[InlineData("boolean")]
189+
[InlineData("uuid")]
190+
[InlineData("vector2")]
191+
[InlineData("vector3")]
192+
[InlineData("vector4")]
193+
[InlineData("vector2Int")]
194+
[InlineData("vector3Int")]
195+
public void AllValidTypesTest(string validType)
196+
{
197+
var schema = $"""
198+
id: testSchema
199+
type: object
200+
properties:
201+
- key: myProperty
202+
type: {validType}
203+
""";
204+
205+
var diagnosticsArray = Test.Generate(schema).analysis.DiagnosticsList;
206+
207+
Assert.DoesNotContain(diagnosticsArray, d => d is UnknownTypeDiagnostics);
208+
}
209+
}

0 commit comments

Comments
 (0)