Skip to content

Swagger Generation not respecting overrided schema for type (via MapType<T>) #309

@lestevaos

Description

@lestevaos

Hello!

I have an Id type that consists in something like <resource-type>_<guid> that is known by it's string form, like aspnet_00995ee0f9804f2f88eaa99f468023e5. I made an specific JsonConverter to work with it and it's serializing and deserializing like a charm. I'm using this type as my id route parameter (from the path portion ["{controller}/{action}/{id}" is my default route]) instead of the common MVC string id.

I'm overriding this specific type to be seen as a string, as presented here.

services.AddSwaggerGen(options =>
{
    options.MapType<Id>(() => new Schema { Pattern = "^[a-z0-9]+_[a-f0-9]{32}$", Type = "string" });
});

The generated paths that are using this type as a parameter in the path portion are not being properly described, as seen below:

...
"/websites/{id}": {
  "get": {
    "tags": ["Websites"],
    "operationId": "WebsitesByIdGet",
    "consumes": [],
    "produces": ["text/plain", "application/json", "text/json"],
    "parameters": [{
      "name": "Guid",
      "in": "path",
      "required": true,
      "type": "string"
    }, {
      "name": "ResourceType",
      "in": "path",
      "required": true,
      "type": "string"
    }, {
      "name": "Value",
      "in": "path",
      "required": true,
      "type": "string"
    }, {
      "name": "id",
      "in": "path",
      "required": true,
      "type": "string"
    }],
...

The Guid, ResourceType and Value parameters are, in fact, properties of my Id class. After listing these unwanted parameters (because I already set that I want the Id type to be treated as a simple string), it ends with the almost correct id parameter definition (it lacks the pattern I set on my specific Schema for this type).

It should be only listing

[{
  "name": "id",
  "in": "path",
  "required": true,
  "type": "string",
  "pattern": "^[a-z0-9]+_[a-f0-9]{32}$"
}]

here, right?

When this Id type of mine is used as a property of another type (like in the website type), the definitions portion of the document shows the Id type definition correct:

...
"api.company.com.models.website": {
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "readOnly": true
    },
    "id": {
      "pattern": "^[a-z0-9]+_[a-f0-9]{32}$",
      "type": "string"
    },
    "identifier": {
      "type": "string"
    }
  }
}
...

Let me know if you need more details or if there's something I can do to help in that.
swagger.json

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions