RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.38k stars 532 forks source link

Prevent reference generation for selected complex class properties #1658

Closed nautnatic closed 8 months ago

nautnatic commented 9 months ago

Hi, I'm trying to generate a JSON schema for a C# class. While I generally want to generate references for complex properties (type class), I have some classes that are only used as a property once. Therefore, I don't want to create a separate model in the JSON schema, but nest it within the owning class to decrease complexity.

Here a small example:

C# classes

public class BaseClass
{
    public NestedClass NestedProperty { get; set; }
}

public class NestedClass
{
    public string Property1 { get; set; }
}

Actually generated JSON schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "BaseClass",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "nestedProperty": {
      "$ref": "#/definitions/NestedClass"
    }
  },
  "definitions": {
    "NestedClass": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "property1": {
          "type": "string"
        }
      }
    }
  }
}

Desired JSON schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "BaseClass",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "nestedProperty": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "property1": {
          "type": "string"
        }
      }
    }
  }
}

Is there some way to achieve this in NJsonSchema? Happy about all suggestions. :)

emillno commented 8 months ago

I think the solution to what you're trying to achieve can be found in this issue: https://github.com/RicoSuter/NJsonSchema/issues/889

nautnatic commented 8 months ago

Yeah it sounds like the same issue. Will close this issue.