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

How to map a c# dictionary<int, string> swagger #844

Closed
daxu7509 opened this issue Aug 16, 2016 · 1 comment
Closed

How to map a c# dictionary<int, string> swagger #844

daxu7509 opened this issue Aug 16, 2016 · 1 comment

Comments

@daxu7509
Copy link

daxu7509 commented Aug 16, 2016

I have a web api which returns a object that contains dictionary property

public Dictionary<int, string> Roles { get; set; }

I use swashbuckle to publish it into swagger format and this is what I get:

"Roles":{"type":"object","additionalProperties":{"type":"string"}}

I am not really sure this is correct? As I thought at least it should be something like

"Roles":{"type":"object","additionalProperties":{"type":"int", "type":"string"}}
How can represent my dictionary correctly in swagger format?

@domaindrivendev
Copy link
Owner

This is the correct description. Remember that Swagger is describing JSON structures and not C# types. In JSON, there's no equivalent of Dictionary<int, string>. The closest representation would be the following:

{
  "1": "foo",
  "2": "bar"
}

And sure enough if you return an object of this type from a WebAPI action, it will be serialized exactly like this - try for yourself. Furthermore, the JSON Schema that Swashbuckle is generating describes this structure accurately:

{"type":"object","additionalProperties":{"type":"string"}}

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

2 participants