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

MinItems and MaxItems attributes generates invalid schema inside array type items of strings #551

Closed
1 task done
domchlouba opened this issue Nov 3, 2023 · 1 comment · Fixed by #553
Closed
1 task done
Labels
bug Something isn't working pkg:schema-generation

Comments

@domchlouba
Copy link

Nuget Package

JsonSchema.Net.Generation

Package Version

3.4.1

Operating System

Windows

.Net Target (if relevant, please specify the version in the description)

.Net (5 or after)

A clear and concise description of the bug.

According to the specification and the description on the package page, the MaxItems and MinItems attributes are valid for an array (or collection of items). However, when generating a schema for a class containing a list of strings with MinItems and MaxItems attributes, these keywords are also part of the items object in the schema.

What did you expect?

I would expect the following result, i.e. that minItems and maxItems are listed only in the object describing the array type and the object in the items property no longer contains these keywords.

Expected result of generator

{
  "type": "object",
  "properties": {
    "Items": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "minItems": 0,
      "maxItems": 2
    }
  }
}

Please add test code or steps to reproduce the behavior.

Model

public class Item
{
    [MinItems(0)]
    [MaxItems(2)]
    public List<string> Items { get; set; }
}

Execution

var schema = new JsonSchemaBuilder().FromType<Item>().Build();

Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(schema, new JsonSerializerOptions() { WriteIndented = true }));

Result of generator

{
  "type": "object",
  "properties": {
    "Items": {
      "type": "array",
      "items": {
        "type": "string",
        "minItems": 0,
        "maxItems": 2
      },
      "minItems": 0,
      "maxItems": 2
    }
  }
}

Is there any other information you'd like to share regarding this bug?

During my debugging I came across the following condition inside MaxItemsAttribute (and MinItemsAttribute) in AddConstraints method:

if (!context.Type.IsArray()) return;

The extension method itself has the following implementation:

public static bool IsArray(this Type type)
{
	return type.IsArray ||
		   type == typeof(Array) ||
		   typeof(IEnumerable).IsAssignableFrom(type);
}

However, the string type also satisfies the condition typeof(IEnumerable).IsAssignableFrom(type)

Code of Conduct

  • I agree to follow this project's Code of Conduct
@domchlouba domchlouba added the bug Something isn't working label Nov 3, 2023
@gregsdennis
Copy link
Owner

Strictly speaking, what's returned is a valid schema; those extra keywords just aren't doing anything.

I'll have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:schema-generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants