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

Swagger generation for JObject fails (due to IDictionary cast) #29

Closed
JoschaMetze opened this issue Apr 23, 2019 · 6 comments
Closed

Swagger generation for JObject fails (due to IDictionary cast) #29

JoschaMetze opened this issue Apr 23, 2019 · 6 comments
Labels
bug open api Open API and Swagger UI related issue

Comments

@JoschaMetze
Copy link

If a model class for an operation contains a JObject property (or JObject itself) the generation fails with an index out of bounds exception. I suppose this is linked to schema.AdditionalProperties = type.GetGenericArguments()[1].ToOpenApiSchema();(line 61 of OpenApiSchemaExtensions.cs) as JObject implements IDictionary but has no generic arguments itself.

I suggest either checking for JObject itself (as you are already referencing Newtonsoft.Json in that file) or checking if the count of arguments from GetGenericArguments exceeds 1.

Thanks
Joscha

@justinyoo
Copy link
Contributor

@JoschaMetze Thanks for the comment! Would you give me some example code snippet so that I can reproduce the error on my side, please?

@JoschaMetze
Copy link
Author

Hey, of course, minimal repro:
does work

 [OpenApiOperation("FunctionWorks")] 
        [OpenApiResponseBody(HttpStatusCode.Unauthorized, "text/plain", typeof(string))]
        [OpenApiResponseBody(HttpStatusCode.OK, "application/json", typeof(string))]
public static object FunctionWorks(){}

does not work

 [OpenApiOperation("FunctionFails")] 
        [OpenApiResponseBody(HttpStatusCode.Unauthorized, "text/plain", typeof(string))]
        [OpenApiResponseBody(HttpStatusCode.OK, "application/json", typeof(JObject))]
public static object FunctionFails(){}

justinyoo added a commit that referenced this issue May 30, 2019
- Not to include Dictionary and JObject at the root level
@justinyoo
Copy link
Contributor

@JoschaMetze Both JObject and Dictionary can't be declared at the root level like the example code above, because they don't have name. But both can be a property of a class like:

public Dictionary<string, string> Collection { get; set; } = new Dictionary<string, string>();

public JObject JsonObject { get; set; } = new JObject();

@justinyoo
Copy link
Contributor

@JoschaMetze I'll close this for now. If you need further discussion, please feel free to open this again.

@justinyoo
Copy link
Contributor

@JoschaMetze Now both JObject and JToken are properly rendered.

@justinyoo justinyoo added bug open api Open API and Swagger UI related issue labels Jun 1, 2019
@JoschaMetze
Copy link
Author

Thanks for fixing that 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug open api Open API and Swagger UI related issue
Projects
None yet
Development

No branches or pull requests

2 participants