Skip to content

Fix bug that causes the configuration version to go out of enum bounds when reading from environment #669

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

xIceFox
Copy link

@xIceFox xIceFox commented Oct 10, 2024

Short Description: This PR fixes a bug that causes the property "version" in the default configuration to go out of enum bounds, caused by a missing enum validation.

Bug: If you input a single integer as a value for the environment variable: "OpenApi__Version", the integer slips into the property, despite being out of enum bounds.

Reproduction: Use this as an environment variable before running: "OpenApi__Version=3"

Consequence: Error in the SwaggerUI when rendering, because /api/swagger.json throws exception.

image

Stack-Trace when calling /api/swagger.json:

Invalid OpenAPI version

   at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions.OpenApiDocumentExtensions.Serialise(OpenApiDocument document, TextWriter writer, OpenApiSpecVersion version, OpenApiFormat format) in /Users/<user>/Code/azure-functions-openapi-extension/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Extensions/OpenApiDocumentExtensions.cs:line 40
   at Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Document.Render(OpenApiSpecVersion version, OpenApiFormat format) in /Users/<user>/Code/azure-functions-openapi-extension/src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/Document.cs:line 245
   at Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Document.<>c__DisplayClass18_0.<RenderAsync>b__0() in /Users/<user>/Code/azure-functions-openapi-extension/src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/Document.cs:line 220
   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.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Document.RenderAsync(OpenApiSpecVersion version, OpenApiFormat format) in /Users/<user>/Code/azure-functions-openapi-extension/src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/Document.cs:line 219
   at Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Functions.OpenApiTriggerFunction.RenderSwaggerDocument(HttpRequestData req, String extension, FunctionContext ctx) in /Users/<user>/Code/azure-functions-openapi-extension/src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/Functions/OpenApiTriggerFunction.cs:line 59

Reason:
Excerpt from the method documentation of Enum.TryParse<>():

result — When this method returns, result contains an object of type TEnum whose value is represented by value if the parse operation succeeds. If the parse operation fails, result contains the default value of the underlying type of TEnum. Note that this value need not be a member of the TEnum enumeration. This parameter is passed uninitialized.

Fix: Validate the enum value and fallback to version 2, if out of bounds.

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

Successfully merging this pull request may close these issues.

1 participant