From 5300508351847742cbf9f0e58f26c4fbc17422f9 Mon Sep 17 00:00:00 2001 From: Alexey Markov Date: Thu, 12 Mar 2020 10:25:50 +0100 Subject: [PATCH] added OpenAPI schema for route parameters --- .../Implementation/OpenApi/OpenApiCompiler.cs | 54 ++++++++++++++----- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/Source/FunctionMonkey.Compiler.Core/Implementation/OpenApi/OpenApiCompiler.cs b/Source/FunctionMonkey.Compiler.Core/Implementation/OpenApi/OpenApiCompiler.cs index 194da543..b63d8299 100644 --- a/Source/FunctionMonkey.Compiler.Core/Implementation/OpenApi/OpenApiCompiler.cs +++ b/Source/FunctionMonkey.Compiler.Core/Implementation/OpenApi/OpenApiCompiler.cs @@ -341,22 +341,52 @@ private static void CreateOperationsFromRoutes( }); } - foreach (HttpParameter property in functionByRoute.RouteParameters) + if (functionByRoute.RouteParameters.Any()) { - var parameter = new OpenApiParameter + var schema = registry.GetOrCreateSchema(commandType); + foreach (HttpParameter property in functionByRoute.RouteParameters) { - Name = property.RouteName.ToCamelCase(), - In = ParameterLocation.Path, - Required = !property.IsOptional, - Schema = property.Type.MapToOpenApiSchema(), - Description = "" - }; + var propertyInfo = commandType.GetProperty(property.Name); - FilterParameter(compilerConfiguration.ParameterFilters, parameter); + // Property Name + var propertyName = propertyInfo.GetAttributeValue((JsonPropertyAttribute attribute) => attribute.PropertyName); + if (string.IsNullOrWhiteSpace(propertyName)) + { + propertyName = propertyInfo.GetAttributeValue((DataMemberAttribute attribute) => attribute.Name); + } + if (string.IsNullOrWhiteSpace(propertyName)) + { + propertyName = propertyInfo.Name.ToCamelCase(); + } - operation.Parameters.Add(parameter); - // TODO: We need to consider what to do with the payload model here - if its a route parameter - // we need to ignore it in the payload model + // Property Required + var propertyRequired = !property.IsOptional; + if (!propertyRequired) + { + propertyRequired = propertyInfo.GetAttributeValue((JsonPropertyAttribute attribute) => attribute.Required) == Required.Always; + } + if (!propertyRequired) + { + propertyRequired = propertyInfo.GetAttributeValue((RequiredAttribute attribute) => attribute) != null; + } + + var propertySchema = schema.Properties[propertyName]; + + var parameter = new OpenApiParameter + { + Name = property.RouteName.ToCamelCase(), + In = ParameterLocation.Path, + Required = propertyRequired, + Schema = propertySchema, + Description = propertySchema.Description + }; + + FilterParameter(compilerConfiguration.ParameterFilters, parameter); + + operation.Parameters.Add(parameter); + // TODO: We need to consider what to do with the payload model here - if its a route parameter + // we need to ignore it in the payload model + } } if (method == HttpMethod.Post || method == HttpMethod.Put || method == HttpMethod.Patch)