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

Description for property in schema is not respected #291

Open
marekott opened this issue Apr 22, 2022 · 4 comments
Open

Description for property in schema is not respected #291

marekott opened this issue Apr 22, 2022 · 4 comments

Comments

@marekott
Copy link

marekott commented Apr 22, 2022


TLTR
It is impossible to set seperate descriptions for properties of the same type where one is described by $ref and also description for such propertie is not respected beside it is a valid case. You can find an example of such structure here: https://json-schema.org/learn/getting-started-step-by-step.html.

"warehouseLocation": {
  "description": "Coordinates of the warehouse where the product is located.",
  "$ref": "https://example.com/geographical-location.schema.json"
}

Hi @JamesNK,
after our last conversation about reading comments from code and puting them in description fields of schema I was working on such feature in my application. I was doing something like this:

var generator = new JSchemaGenerator
{
    SchemaIdGenerationHandling = SchemaIdGenerationHandling.None,
    SchemaLocationHandling = SchemaLocationHandling.Inline,
    SchemaReferenceHandling = SchemaReferenceHandling.Objects,
};
var schema = generator.Generate(typeof(User));

int i = 0;
foreach (var schemaProperty in schema.Properties)
{
    schemaProperty.Value.Description = $"{schemaProperty.Key}:{i++}";
}
Console.WriteLine(schema.ToString(SchemaVersion.Draft7));

public class User
{
    public int Age { get; set; }
    public Address Address1 { get; set; }
    public Address Address2 { get; set; }
}

public class Address
{
    public string Street { get; set; }
}

Expected result:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "Age": {
      "description": "Age:0",
      "type": "integer"
    },
    "Address1": {
      "description": "Address1:1",
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Street": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "Street"
      ]
    },
    "Address2": {
      "$ref": "#/properties/Address1",
      "description": "Address2:2",
    }
  },
  "required": [
    "Age",
    "Address1",
    "Address2"
  ]
}

Actual result:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "Age": {
      "description": "Age:0",
      "type": "integer"
    },
    "Address1": {
      "description": "Address2:2",
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Street": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "Street"
      ]
    },
    "Address2": {
      "$ref": "#/properties/Address1"
    }
  },
  "required": [
    "Age",
    "Address1",
    "Address2"
  ]
}

Altering description on Address2 is also overriding description for Address1. Whats more, int the end description for Address2 is not included. Is it expected bahaviour or bug? If is is expected how can I set seperate descriptions for both Addresses and also include the one that is correlated with property described by $ref?

@marekott
Copy link
Author

marekott commented Apr 22, 2022

Also, I tried to check how you handle it when properties are decorated with DescriptionAttribute (https://www.newtonsoft.com/jsonschema/help/html/GenerateWithDescriptions.htm). According to your documentation, in order to add descriptions I should do it as follow.

public class User
{
    public int Age { get; set; }
    [Description("This is Address1")]
    public Address Address1 { get; set; }
    [Description("This is Address2")]
    public Address Address2 { get; set; }
}

Expected result:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "Age": {
      "type": "integer"
    },
    "Address1": {
      "description": "This is Address1",
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Street": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "Street"
      ]
    },
    "Address2": {
      "$ref": "#/properties/Address1",
      "description": "This is Address2",
    }
  },
  "required": [
    "Age",
    "Address1",
    "Address2"
  ]
}

Actual result:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "Age": {
      "type": "integer"
    },
    "Address1": {
      "description": "This is Address1",
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Street": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "Street"
      ]
    },
    "Address2": {
      "description": "This is Address2",
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Street": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "Street"
      ]
    }
  },
  "required": [
    "Age",
    "Address1",
    "Address2"
  ]
}

Why after using mentioned attribute SchemaReferenceHandling.Objects is not respected? Is it a bug or expected behaviour? It is a problem for me because based on schema (using external library) I am generating sample C# class for documentation purpose. When Address2 is not described by reference it is treated as seperate class and duplicate is generated.

@plachor
Copy link

plachor commented Apr 27, 2022

Hi @JamesNK any comment on this topic? We would love to save schema with all objects referenced using ref but still we need to keep per property description as @marekott stated above. Current behavior looks like a bug.

@marekott
Copy link
Author

Hi @JamesNK, any thoughts?

@DrG0rg
Copy link

DrG0rg commented Nov 8, 2023

Are there any updates?
I am having the issue myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants