azure-functions-openapi-extension icon indicating copy to clipboard operation
azure-functions-openapi-extension copied to clipboard

OpenApiResponseWithBody containing property named `parameters` throws exception

Open keenfann opened this issue 2 years ago • 6 comments

Describe the issue Using a class that contains a property named parameters in the OpenApiResponseWithBody attribute crashes the ../swagger.json endpoint.

Using version 1.3.0.

To Reproduce Decorate a function with this:

[OpenApiOperation]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(Output))]

The class Output looks like this

public class Output
{
    public string parameters { get; set; }
}

Returns this when calling /swagger.json endpoint:

Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.

   at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
   at Newtonsoft.Json.Linq.Extensions.Convert[T,U](IEnumerable`1 source)+MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToList()
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Document.Render(OpenApiSpecVersion version, OpenApiFormat format)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Document.<>c__DisplayClass18_0.<RenderAsync>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Document.RenderAsync(OpenApiSpecVersion version, OpenApiFormat format)
   at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiTriggerFunctionProvider.RenderSwaggerDocument(HttpRequest req, String extension, ExecutionContext ctx, ILogger log)

keenfann avatar Aug 19 '22 10:08 keenfann

@KeenFann Thanks for the issue! If you change the property name to something else, say MyParameters and give it a decorator of [JsonProperty("parameters")], does that still throw an exception?

justinyoo avatar Aug 22 '22 15:08 justinyoo

Yes, using [JsonProperty("parameters")] with a different property name still throws the exception.

keenfann avatar Aug 24 '22 07:08 keenfann

Had same issue upgrading to the latest version 1.4.0 of [Microsoft.Azure.WebJobs.Extensions.OpenApi] in my Azure function running .Net6, where I created a model-class named [Parameters]. Seems that the the property [Parameters] is a reserved one. Renaming as below resolved the problem and the OpenApi document could be generated again.

public class Parameters

{
     [JsonProperty("parameters")]
     public List<Parameter> List { get; set; }     
 } 

to my-test:

public class Xarameters
 {
     [JsonProperty("xarameters")]
     public List<Parameter> List { get; set; }    
 }

steenalstrup avatar Oct 19 '22 13:10 steenalstrup

Got the same issue using version 1.5.0.

Downgrading to version 1.1.0 solved the issue which seems to have been introduced in 1.2.0

singingknight avatar Jan 05 '23 12:01 singingknight

I have a similar issue. I'm trying to use a multipart/form-data with:

[OpenApiRequestBody(contentType: "multipart/form-data", bodyType: typeof(MultiPartFormData))]

the MultiPartFormData is like:

public class MultiPartFormData
    {
        public byte[] File { get; set; }
    }

Using [Microsoft.Azure.WebJobs.Extensions.OpenApi] version 1.1.0 and .Net6 the parameter is not shown in the swagger. Upgrading [Microsoft.Azure.WebJobs.Extensions.OpenApi] to 1.2.0 or earlier then throw the exception:

Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.

jcezardasilva avatar Mar 20 '23 18:03 jcezardasilva

Same Issue on 1.5.1 downgrading to 1.1.0 solved the problem

planUnd avatar Apr 03 '24 15:04 planUnd