From 26845448c5e258610252f9aa9f17f320f9864749 Mon Sep 17 00:00:00 2001 From: ViktoriiaZheliezniak Date: Fri, 7 Jun 2024 15:59:46 +0300 Subject: [PATCH 1/4] Fixed SystemTextJson indexed properties handling. --- ...temTextJsonExtensionDataGenerationTests.cs | 67 ++++++++++++++++++- .../SystemTextJsonReflectionService.cs | 6 ++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs index 0ebc39939..0a947d1c4 100644 --- a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; using NJsonSchema.Generation; @@ -24,6 +25,56 @@ public class ClassWithJsonElementExtensionData public IDictionary ExtensionData { get; set; } } + public class ClassWithIndexedProperty + { + public double X { get; set; } + public double Y { get; set; } + + public double this[int indexer] + { + get + { + switch (indexer) + { + case 0: return X; + case 1: return Y; + default: throw new ArgumentOutOfRangeException(nameof(indexer)); + } + } + set + { + switch (indexer) + { + case 0: X = value; break; + case 1: Y = value; break; + default: throw new ArgumentOutOfRangeException(nameof(indexer)); + } + } + } + + public double this[string indexer] + { + get + { + switch (indexer) + { + case "X": return X; + case "Y": return Y; + default: throw new ArgumentOutOfRangeException(nameof(indexer)); + } + } + set + { + switch (indexer) + { + case "X": X = value; break; + case "Y": Y = value; break; + default: throw new ArgumentOutOfRangeException(nameof(indexer)); + } + } + } + } + [Fact] public void SystemTextJson_When_class_has_object_Dictionary_with_JsonExtensionDataAttribute_on_property_then_AdditionalProperties_schema_is_set() { @@ -53,5 +104,19 @@ public void SystemTextJson_When_class_has_JsonElement_Dictionary_with_JsonExtens Assert.True(schema.AllowAdditionalProperties); Assert.True(schema.AdditionalPropertiesSchema.ActualSchema.IsAnyType); } + + [Fact] + public void SystemTextJson_When_class_has_Indexed_properties_then_Generates_schema_without_them() + { + // Act + var schema = JsonSchemaGenerator.FromType(new SystemTextJsonSchemaGeneratorSettings + { + SchemaType = SchemaType.JsonSchema + }); + + // Assert + Assert.Equal(2, schema.ActualProperties.Count); + Assert.All(schema.ActualProperties, property => Assert.False(string.Equals(property.Key, "Item", StringComparison.InvariantCultureIgnoreCase))); + } } } \ No newline at end of file diff --git a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs index 71cb2cea8..bbd4c2774 100644 --- a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs +++ b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs @@ -45,6 +45,12 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex continue; } + if (accessorInfo.MemberInfo is PropertyInfo propInfo && + propInfo.GetIndexParameters().Length > 0) + { + continue; + } + var propertyIgnored = false; var jsonIgnoreAttribute = accessorInfo .GetAttributes(true) From d7297d54b20a2aa159923dcc20fbebbc95a4fcb5 Mon Sep 17 00:00:00 2001 From: ViktoriiaZheliezniak Date: Mon, 10 Jun 2024 10:22:05 +0300 Subject: [PATCH 2/4] Fixed CA1859 error. --- src/NJsonSchema/Generation/JsonSchemaGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs index 6e8314813..43f21e4cc 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs @@ -1089,7 +1089,7 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema schema, Json } } - private object? TryGetInheritanceDiscriminatorConverter(Type type) + private SystemTextJsonInheritanceWrapper? TryGetInheritanceDiscriminatorConverter(Type type) { var typeAttributes = type.GetTypeInfo().GetCustomAttributes(false).OfType(); From 1e3860aac6b4ed2f7a28bdbabf1294e3f5e87c0a Mon Sep 17 00:00:00 2001 From: ViktoriiaZheliezniak Date: Mon, 10 Jun 2024 17:11:38 +0300 Subject: [PATCH 3/4] Revert "Fixed CA1859 error." This reverts commit d7297d54b20a2aa159923dcc20fbebbc95a4fcb5. --- src/NJsonSchema/Generation/JsonSchemaGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs index 43f21e4cc..6e8314813 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs @@ -1089,7 +1089,7 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema schema, Json } } - private SystemTextJsonInheritanceWrapper? TryGetInheritanceDiscriminatorConverter(Type type) + private object? TryGetInheritanceDiscriminatorConverter(Type type) { var typeAttributes = type.GetTypeInfo().GetCustomAttributes(false).OfType(); From 0e3403202646f00e65711377de1cfe4a8473465f Mon Sep 17 00:00:00 2001 From: ViktoriiaZheliezniak Date: Tue, 11 Jun 2024 11:56:41 +0300 Subject: [PATCH 4/4] Disabled check for CA1859. --- src/NJsonSchema/Generation/JsonSchemaGenerator.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs index 6e8314813..f973f87b3 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs @@ -1089,6 +1089,7 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema schema, Json } } +#pragma warning disable CA1859 private object? TryGetInheritanceDiscriminatorConverter(Type type) { var typeAttributes = type.GetTypeInfo().GetCustomAttributes(false).OfType(); @@ -1099,8 +1100,8 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema schema, Json { var converterType = (Type)jsonConverterAttribute.ConverterType; if (converterType != null && ( - converterType.IsAssignableToTypeName("JsonInheritanceConverter", TypeNameStyle.Name) || // Newtonsoft's converter - converterType.IsAssignableToTypeName("JsonInheritanceConverter`1", TypeNameStyle.Name) // System.Text.Json's converter + converterType.IsAssignableToTypeName("JsonInheritanceConverter", TypeNameStyle.Name) || // Newtonsoft's converter + converterType.IsAssignableToTypeName("JsonInheritanceConverter`1", TypeNameStyle.Name) // System.Text.Json's converter )) { return ObjectExtensions.HasProperty(jsonConverterAttribute, "ConverterParameters") && @@ -1125,6 +1126,8 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema schema, Json return null; } +#pragma warning restore CA1859 + private sealed class SystemTextJsonInheritanceWrapper {