I have a function defined as follows:
public class RegistrationFunction(
IUserExistsQuery userExistsQuery,
IRegisterUserCommand registerUser)
{
[Function(nameof(RegistrationFunction))]
[OpenApiOperation(operationId: "RegisterUser", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "x-functions-key", In = OpenApiSecurityLocationType.Header)]
[OpenApiRequestBody(contentType: MediaTypeNames.Application.Json, bodyType: typeof(UserDataRequestBody))]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.OK)]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.Conflict)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, bodyType: typeof(RegistrationErrorResponseBody), contentType: MediaTypeNames.Application.Json)]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req,
[FromBody] UserDataRequestBody userData)
{
// code removed for brevity
}
}
I’m also using the right package reference to enable OpenApi docs:
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.5.1" />
And my Program.cs contains this code, which I also think is correct:
var host = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterType<UserExistsQuery>().AsImplementedInterfaces().SingleInstance();
builder.RegisterType<RegisterUserCommand>().AsImplementedInterfaces().SingleInstance();
})
.ConfigureServices(services =>
{
services.AddDbContext<MLTRContext>(options =>
{
var connectionString = Environment.GetEnvironmentVariable("PostgresConnectionString");
options.UseNpgsql(connectionString);
});
services.AddSingleton<IOpenApiConfigurationOptions>(_ =>
{
var options = new OpenApiConfigurationOptions
{
Info = new OpenApiInfo
{
Version = DefaultOpenApiConfigurationOptions.GetOpenApiDocVersion(),
Title = $"{DefaultOpenApiConfigurationOptions.GetOpenApiDocTitle()} (Injected)",
Description = DefaultOpenApiConfigurationOptions.GetOpenApiDocDescription(),
TermsOfService = new Uri("https://github.com/Azure/azure-functions-openapi-extension"),
Contact = new OpenApiContact
{
Name = "Enquiry",
Email = "[email protected]",
Url = new Uri("https://github.com/Azure/azure-functions-openapi-extension/issues"),
},
License = new OpenApiLicense
{
Name = "MIT",
Url = new Uri("http://opensource.org/licenses/MIT"),
}
},
Servers = DefaultOpenApiConfigurationOptions.GetHostNames(),
OpenApiVersion = DefaultOpenApiConfigurationOptions.GetOpenApiVersion(),
IncludeRequestingHostName = DefaultOpenApiConfigurationOptions.IsFunctionsRuntimeEnvironmentDevelopment(),
ForceHttps = DefaultOpenApiConfigurationOptions.IsHttpsForced(),
ForceHttp = DefaultOpenApiConfigurationOptions.IsHttpForced(),
};
return options;
});
})
.ConfigureFunctionsWorkerDefaults(w =>
w
.UseMiddleware<ErrorExposingMiddleware>()
.UseMiddleware<PrintEnvVarsMiddleware>())
.Build();
await host.RunAsync();
However, when I build and run the code locally, no OpenApi endpoints appear in the console, and if I try going to /api/swagger/ui, I get 404. May it be because my code layout is a little bit non-standard?
| - Program.cs
| - local.settings.json
| | - Functions
| | | - Registration
| | | | - RegistrationFunction.cs