Skip to content

Commit 120020a

Browse files
committed
Add Preserve to constructors. Clone members to fix bug on IL2CPP
1 parent 3d2f4db commit 120020a

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

Packages/net.yutopp.vjson/Runtime/Schema/JsonSchema.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public JsonSchemaAttribute()
261261
{
262262
}
263263

264+
[Preserve]
264265
public JsonSchemaAttribute(bool b)
265266
{
266267
if (!b)
@@ -381,7 +382,7 @@ public static JsonSchemaAttribute CreateFromType(Type ty, JsonSchemaRegistry reg
381382
reg = new JsonSchemaRegistry();
382383
}
383384

384-
var schema = TypeHelper.GetCustomAttribute<JsonSchemaAttribute>(ty);
385+
var schema = (JsonSchemaAttribute) TypeHelper.GetCustomAttribute<JsonSchemaAttribute>(ty)?.MemberwiseClone(); // On IL2CPP, returns same instance, thus call MemberwiseClone.
385386
if (schema == null)
386387
{
387388
schema = new JsonSchemaAttribute();

Packages/net.yutopp.vjson/Tests/Runtime/Schema/SchemaTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public void SchemaFormatTest(Type ty, string expected)
8080
Assert.AreEqual(expected, schema.ToString());
8181
}
8282

83+
public abstract class Gb2 {}
84+
85+
[JsonSchema(Id = "g2")]
86+
public sealed class G2 : Gb2 {}
87+
8388
public static object[] SchemaStringArgs = new object[] {
8489
new object[] {
8590
typeof(int),
@@ -241,6 +246,16 @@ public void SchemaFormatTest(Type ty, string expected)
241246
typeof(ValidatorWithSerializerTests.CustomTag<float>),
242247
"{\"type\":\"number\",\"minimum\":0}",
243248
},
249+
// ↓ Run same testcases twice, it will be broken on IL2CPP. Thus added as test cases.
250+
new object[] {
251+
typeof(G2),
252+
@"{""$id"":""g2"",""type"":""object"",""properties"":{},""allOf"":[{""$ref"":""VJson.Schema.UnitTests.JsonSchemaFormatTests+Gb2""}]}",
253+
},
254+
new object[] {
255+
typeof(G2),
256+
@"{""$id"":""g2"",""type"":""object"",""properties"":{},""allOf"":[{""$ref"":""VJson.Schema.UnitTests.JsonSchemaFormatTests+Gb2""}]}",
257+
},
258+
// ↑
244259
};
245260
}
246261

0 commit comments

Comments
 (0)