title: Swashbuckle SwaggerGen ignores nullable annotations on referenced objects
body: <p>We are using <a href="https://www.nuget.org/packages/swashbuckle.aspnetcore.swaggergen/" rel="nofollow noreferrer">Swashbuckle SwaggerGen</a> version 5.6.3.0 to generate Swagger API documentation for our Rest API. Everything works fine, except for the fact that the generator ignores <code>nullable</code> annotations on custom object types.</p>
  <p>Consider the following code:</p>
  <pre><code>public class Vector3f
  {
      public float X { get; set; }
      public float Y { get; set; }
      public float Z { get; set; }
  }
  
  public class MyClass
  {
      [JsonProperty(Required = Required.Always)]
      public string NameAlways { get; set; }
  
      [JsonProperty(Required = Required.AllowNull)]
      public string NameAllowNull { get; set; }
  
      [JsonProperty(Required = Required.DisallowNull)]
      public string NameDisallowNull { get; set; }
  
      [JsonProperty(Required = Required.Default)]
      public string NameDefault { get; set; }
  
  
      [JsonProperty(Required = Required.Always)]
      public Vector3f VectorAlways { get; set; }
  
      [JsonProperty(Required = Required.AllowNull)]
      public Vector3f VectorAllowNull { get; set; }
  
      [JsonProperty(Required = Required.DisallowNull)]
      public Vector3f VectorDisallowNull { get; set; }
  
      [JsonProperty(Required = Required.Default)]
      public Vector3f VectorDefault { get; set; }
  }
  </code></pre>
  <p>With <code>string</code>, everything works as expected, <code>NameAlways</code> and <code>NameAllowNull</code> are marked as &quot;required&quot;, and <code>NameAllowNull</code> and <code>NameDefault</code> are marked as &quot;nullable&quot;. However with <code>Vector3f</code> only &quot;required&quot; works, and nothing is marked as nullable!</p>
  <p>Here is the relevant part of the generated JSON:</p>
  <pre><code>&quot;MyClass&quot;: {
    &quot;required&quot;: [
      &quot;nameAllowNull&quot;,
      &quot;nameAlways&quot;,
      &quot;vectorAllowNull&quot;,
      &quot;vectorAlways&quot;
    ],
    &quot;type&quot;: &quot;object&quot;,
    &quot;properties&quot;: {
      &quot;nameAlways&quot;: {
        &quot;type&quot;: &quot;string&quot;
      },
      &quot;nameAllowNull&quot;: {
        &quot;type&quot;: &quot;string&quot;,
        &quot;nullable&quot;: true
      },
      &quot;nameDisallowNull&quot;: {
        &quot;type&quot;: &quot;string&quot;
      },
      &quot;nameDefault&quot;: {
        &quot;type&quot;: &quot;string&quot;,
        &quot;nullable&quot;: true
      },
      &quot;vectorAlways&quot;: {
        &quot;$ref&quot;: &quot;#/components/schemas/Vector3f&quot;
      },
      &quot;vectorAllowNull&quot;: {
        &quot;$ref&quot;: &quot;#/components/schemas/Vector3f&quot;
      },
      &quot;vectorDisallowNull&quot;: {
        &quot;$ref&quot;: &quot;#/components/schemas/Vector3f&quot;
      },
      &quot;vectorDefault&quot;: {
        &quot;$ref&quot;: &quot;#/components/schemas/Vector3f&quot;
      }
    }
  }
  </code></pre>
  <p>As you can see, the &quot;nullable&quot; annotation isn't generated. I have tried writing a custom <code>ISchemaFilter</code>, similar to how it's described <a href="https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1346" rel="nofollow noreferrer">here</a>, but with limited success. Even if I manually set the nullable flag by setting <code>property.Value.Nullable = true</code>, the generator still ignores it and doesn't insert the nullable annotation when there is a <code>$ref</code> in the schema body.</p>
  <p><strong>Workaround</strong></p>
  <p>I did, however, manage to add a new property called <code>x-nullable</code>, which correctly listed nullable properties based on the annotation. It worked OK, but it would obviously be better if the schema itself contained the &quot;nullable&quot; annotation (like string does for example) instead of it being in a separate property.</p>
  <p>Is there a way to solve this? Does Swashbuckle version 6 change anything?</p>
  <p><strong>EDIT:</strong></p>
  <p>I tried to use <code>UseAllOfToExtendReferenceSchemas</code> as described <a href="https://stackoverflow.com/questions/62424769/using-swashbuckle-5-x-specify-nullable-true-on-a-generic-t-parameter-reference">here</a>, and while it works well for the generated JSON, it is kind of unusable for the Web Swagger viewer, because now it deletes the type of the object (Vector3f), and only display it's properties.</p>
  <p>Differences:
  <a href="https://i.stack.imgur.com/ASQ97.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ASQ97.png" alt="Type visible without the setting, but not the nullable annotation" /></a></p>
  <p>It's also important to note that the type is gone only in the Web viewer, and is still present in the JSON with the <code>UseAllOfToExtendReferenceSchemas</code> setting, but without it, the nullable annotation is gone from both. Could the web viewer be somehow configured to display the type as well?</p>