Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed SystemTextJson indexed properties handling. #1701

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,6 +25,56 @@ public class ClassWithJsonElementExtensionData
public IDictionary<string, JsonElement> 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()
{
Expand Down Expand Up @@ -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<ClassWithIndexedProperty>(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)));
}
}
}
2 changes: 1 addition & 1 deletion src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema schema, Json
}
}

private object? TryGetInheritanceDiscriminatorConverter(Type type)
private SystemTextJsonInheritanceWrapper? TryGetInheritanceDiscriminatorConverter(Type type)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really needed?
i think this might break CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think this should be reverted, it breaks test When_schema_has_base_schema_then_it_is_referenced_with_Newtonsoft.

However, without it build fails with " [ERR] Compile: D:\a\NJsonSchema\NJsonSchema\src\NJsonSchema\Generation\JsonSchemaGenerator.cs(1092,25): error CA1859: Change return type of method 'TryGetInheritanceDiscriminatorConverter' from 'object?' to 'NJsonSchema.Generation.JsonSchemaGenerator.SystemTextJsonInheritanceWrapper?' for improved performance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859) [D:\a\NJsonSchema\NJsonSchema\src\NJsonSchema\NJsonSchema.csproj::TargetFramework=netstandard2.0]"."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you suggest a better fix to CA1859?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use

#pragma warning disable CA1859
private object? TryGetInheritanceDiscriminatorConverter(Type type)
#pragma warning enable CA1859

{
var typeAttributes = type.GetTypeInfo().GetCustomAttributes(false).OfType<Attribute>();

Expand Down
6 changes: 6 additions & 0 deletions src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading