title: Change property type as exported by Swagger/Swashbuckle
body: <p>I have a fairly complex object with nested objects; please note that in the example below I have simplified this object greatly.</p>
  
  <p>Assume the following example object:</p>
  
  <pre><code>public class Result {
      public string Name { get; set; }
      public IpAddress IpAddress { get; set; }
  }
  </code></pre>
  
  <p>I have implemented a <code>JsonConverter&lt;IPAddress&gt;</code> than (de)serializes the Ip as a string:</p>
  
  <pre><code>public class IPAddressConverter : JsonConverter&lt;IPAddress&gt;
  {
      public override IPAddress Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
          =&gt; IPAddress.Parse(reader.GetString());
  
      public override void Write(Utf8JsonWriter writer, IPAddress value, JsonSerializerOptions options)
          =&gt; writer.WriteStringValue(value.ToString());
  }
  </code></pre>
  
  <p>The <code>IPAddressConverter</code> was then 'registered' as a converter in the <code>AddJsonOptions(...)</code> method. This nicely returns results as:</p>
  
  <pre><code>{ "Name": "Foo", "IpAddress": "198.51.100.1" }
  </code></pre>
  
  <p>And, vice versa, my controller "understands" IP addresses specified as string:</p>
  
  <pre><code>public IEnumerable&lt;Result&gt; FindByIp(IpAddress ip) {
      // ...
  }
  </code></pre>
  
  <p>However, SwashBuckle exports this as:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "openapi": "3.0.1",
    "info": {
      "title": "Example",
      "version": "v1"
    },
    "paths": {
      "/FindByIp": {
        "get": {
          "parameters": [
            {
              "name": "q",
              "in": "query",
              "schema": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            }
          ],
          "responses": {
            "200": {
              "description": "Success",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "additionalProperties": {
                      "$ref": "#/components/schemas/Result"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "components": {
      "schemas": {
        "AddressFamily": {
          "enum": [
            0,
            1,
            2,
            3,
            4,
            5,
            6,
            6,
            7,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            21,
            22,
            23,
            24,
            25,
            26,
            28,
            29,
            65536,
            65537,
            -1
          ],
          "type": "integer",
          "format": "int32"
        },
        "IPAddress": {
          "type": "object",
          "properties": {
            "addressFamily": {
              "$ref": "#/components/schemas/AddressFamily"
            },
            "scopeId": {
              "type": "integer",
              "format": "int64"
            },
            "isIPv6Multicast": {
              "type": "boolean",
              "readOnly": true
            },
            "isIPv6LinkLocal": {
              "type": "boolean",
              "readOnly": true
            },
            "isIPv6SiteLocal": {
              "type": "boolean",
              "readOnly": true
            },
            "isIPv6Teredo": {
              "type": "boolean",
              "readOnly": true
            },
            "isIPv4MappedToIPv6": {
              "type": "boolean",
              "readOnly": true
            },
            "address": {
              "type": "integer",
              "format": "int64"
            }
          },
          "additionalProperties": false
        },
        "Result": {
          "type": "object",
          "properties": {
            "ip": {
              "$ref": "#/components/schemas/IPAddress"
            },
            "name": {
              "type": "string",
              "nullable": true
            }
          },
          "additionalProperties": false
        }
      }
    }
  }
  </code></pre>
  
  <p>Which, for the more visually inclined, looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/dAyK7.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dAyK7.png" alt="Screenshot"></a></p>
  
  <p>What I'd like to achieve, however, is this:</p>
  
  <pre><code>{
    "openapi": "3.0.1",
    "info": {
      "title": "Example",
      "version": "v1"
    },
    "paths": {
      "/FindByIp": {
        "get": {
          "parameters": [
            {
              "name": "q",
              "in": "query",
              "schema": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            }
          ],
          "responses": {
            "200": {
              "description": "Success",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "additionalProperties": {
                      "$ref": "#/components/schemas/Result"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "components": {
      "schemas": {
        "Result": {
          "type": "object",
          "properties": {
            "ip": {
              "type": "string",
              "nullable": true
            },
            "name": {
              "type": "string",
              "nullable": true
            }
          },
          "additionalProperties": false
        }
      }
    }
  }
  </code></pre>
  
  <p>Again, visualized:</p>
  
  <p><a href="https://i.stack.imgur.com/vI8Gh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/vI8Gh.png" alt="Screenshot"></a></p>
  
  <p>I was hoping to be able to add an annotation / attribute on some properties (so I looked at <a href="https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/master/src/Swashbuckle.AspNetCore.Annotations" rel="noreferrer">Swashbuckle.AspNetCore.Annotations</a>) but that doesn't seem to be possible.</p>
  
  <p>Also, because the object is fairly complex and comes from a 3rd party library it's hard for me to actually add annotations / attributes on properties because I can't change the model (easily).</p>
  
  <p>I <em>could</em> resort to AutoMapper (or alike) to create another model with a string for IP adresses but that would mean having to model all objects in the original model. Besides, it requires extra code and maintenance when the model changes. I'd rather tell Swashbuckle, somehow, that IP adresses (and, so, the type <a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.ipaddress" rel="noreferrer"><code>IPAddress</code></a> will be represented as a string (in- and outgoing to my API). I'm looking for options on how to accomplish this the best way possible within given limitations (preferably not introducing new models to map to, preferably no annotations/attributes because I can't easily access the 3rd party library). Is there a way to register a "type-converter-something" for Swashbuckle to handle this?</p>
  
  <h1>Update: <a href="https://stackoverflow.com/a/58489384/215042">Solved</a>!</h1>
  
  <p>This is what I ended up with:</p>
  
  <pre><code>// This method gets called by the runtime. Use this method to add services to the container.
  public void ConfigureServices(IServiceCollection services)
  {
      services
          .AddResponseCompression()
          .AddMemoryCache()
          .AddControllers()
          // etc...
          // etc...
  
      // Here's the interesting part:
      services.AddSwaggerGen(c =&gt;
          {
              c.SwaggerDoc("v1", new OpenApiInfo { Title = "Example", Version = "v1" });
              c.MapType&lt;IPAddress&gt;(() =&gt; new OpenApiSchema { Type = typeof(string).Name });
              // ...
          });
  }
  </code></pre>
  
  <p>Thank you <a href="https://stackoverflow.com/users/4720542/strickt01">strickt01</a></p>