public ParameterDescriptionEx[] OpenApiParametersToParameterDescriptions(IList <OpenApiParameter> ps) { return(ps.Select(p => { var refinedName = NameFunc.RefineParameterName(p.Name); var r = new ParameterDescriptionEx() { Name = refinedName, //azure.com\apimanagement-apimapis has $ in query parameter QName = p.Name, Documentation = p.Description, ParameterDescriptor = new ParameterDescriptor() { IsOptional = !p.Required, ParameterName = refinedName, ParameterType = TypeRefHelper.PrimitiveSwaggerTypeToClrType(p.Schema.Type, p.Schema.Format), ParameterBinder = ParameterLocationToParameterBinder(p.In), }, ParameterTypeReference = OpenApiParameterToCodeTypeReference(p) }; return r; } ).Where(k => k.ParameterDescriptor.ParameterBinder != ParameterBinder.None).ToArray()); }
/// <summary> /// /// </summary> /// <param name="propertySchema"></param> /// <param name="typeDeclarationName"></param> /// <param name="propertyName"></param> /// <param name="currentTypeName"></param> /// <param name="ns"></param> /// <returns>CodeTypeReference and CasualTypeName. Empty if no casualTypeName.</returns> public Tuple <CodeTypeReference, string> CreateArrayCodeTypeReference(OpenApiSchema propertySchema, string typeDeclarationName, string propertyName, string currentTypeName, string ns) { OpenApiSchema arrayItemsSchema = propertySchema.Items; if (arrayItemsSchema == null) //ritekit.com has parameter as array but without items type. Presumbly it may be string. { Type clrType = TypeRefHelper.PrimitiveSwaggerTypeToClrType("string", null); CodeTypeReference arrayCodeTypeReference = TypeRefHelper.CreateArrayTypeReference(clrType, 1); return(Tuple.Create(arrayCodeTypeReference, String.Empty)); } else if (arrayItemsSchema.Reference != null) //array of custom type { string arrayTypeSchemaRefId = arrayItemsSchema.Reference.Id; var arrayTypeNs = NameFunc.GetNamespaceOfClassName(arrayTypeSchemaRefId); var arrayTypeName = NameFunc.RefineTypeName(arrayTypeSchemaRefId, arrayTypeNs); var arrayTypeWithNs = NameFunc.CombineNamespaceWithClassName(arrayTypeNs, arrayTypeName); var existingType = FindTypeDeclarationInNamespaces(arrayTypeName, arrayTypeNs); if (existingType == null) // Referencing to a type not yet added to namespace { var existingSchema = FindSchema(arrayTypeSchemaRefId); if (existingSchema != null && !RegisteredSchemaRefIdExists(arrayTypeSchemaRefId)) { AddTypeToCodeDom(new KeyValuePair <string, OpenApiSchema>(arrayTypeSchemaRefId, existingSchema)); } } if (TypeAliasDic.TryGet(arrayTypeSchemaRefId, out string arrayTypeNameAlias)) { if (!TypeRefHelper.IsSwaggerPrimitive(arrayTypeNameAlias)) { return(Tuple.Create(ComponentsHelper.CreateArrayOfCustomTypeReference(arrayTypeNameAlias, 1), String.Empty)); } else { var clrType = TypeRefHelper.PrimitiveSwaggerTypeToClrType(arrayTypeNameAlias, null); return(Tuple.Create(ComponentsHelper.CreateArrayOfCustomTypeReference(clrType.FullName, 1), String.Empty)); } } else { return(Tuple.Create(ComponentsHelper.CreateArrayOfCustomTypeReference(arrayTypeWithNs, 1), String.Empty)); } } else { string arrayType = arrayItemsSchema.Type; if (arrayItemsSchema.Enum != null && arrayItemsSchema.Enum.Count > 0) { string[] enumMemberNames; try { enumMemberNames = (String.IsNullOrEmpty(arrayItemsSchema.Type) || arrayItemsSchema.Type == "string") ? arrayItemsSchema.Enum.Cast <OpenApiString>().Select(m => m.Value).ToArray() : arrayItemsSchema.Enum.Cast <OpenApiInteger>().Select(m => "_" + m.Value.ToString()).ToArray(); } catch (InvalidCastException ex) { throw new CodeGenException($"When dealing with {propertyName} of {arrayType}, error: {ex.Message}"); } CodeTypeDeclaration existingDeclaration = FindEnumDeclaration(enumMemberNames); if (existingDeclaration != null) { string existingTypeName = existingDeclaration.Name; CodeTypeReference enumArrayReference = TypeRefHelper.CreateArrayOfCustomTypeReference(existingTypeName, 1); return(Tuple.Create(enumArrayReference, String.Empty)); } //warning about bad yaml design. Trace.TraceWarning($"Property {NameFunc.RefineParameterName(propertyName)} has referenced some enum members {String.Join(", ", enumMemberNames)} which are not of any declared components."); } else if (arrayItemsSchema.Properties != null && arrayItemsSchema.Properties.Count > 0) // for casual type { string casualTypeName = typeDeclarationName + NameFunc.RefinePropertyName(propertyName); CodeTypeDeclaration casualTypeDeclaration = AddTypeToClassNamespace(casualTypeName, ns); //stay with the namespace of the host class AddProperties(casualTypeDeclaration, arrayItemsSchema, currentTypeName, ns); return(Tuple.Create(ComponentsHelper.CreateArrayOfCustomTypeReference(casualTypeName, 1), casualTypeName)); } Type clrType = TypeRefHelper.PrimitiveSwaggerTypeToClrType(arrayType, null); return(Tuple.Create(TypeRefHelper.CreateArrayTypeReference(clrType, 1), String.Empty)); } }
/// <summary> /// Compose action name according to tags, httpMethod and Parameters. /// </summary> /// <param name="op"></param> /// <param name="httpMethod"></param> /// <returns></returns> public static string ComposeActionName(OpenApiOperation op, string httpMethod) { if (op.Tags == null || op.Tags.Count == 0) { throw new ArgumentException("OpenApiOperation does not contain tags for composing action name."); } string byWhat = String.Join("And", op.Parameters.Where(p => p.In == ParameterLocation.Path || p.In == ParameterLocation.Query).Select(p => NameFunc.ToTitleCase(NameFunc.RefineParameterName(p.Name)))); return(ToTitleCase(op.Tags[0].Name) + httpMethod + (String.IsNullOrEmpty(byWhat) ? String.Empty : "By" + byWhat)); }
/// <summary> /// Generate action name hopefully unique across all paths and operations, under a god container class. Consisting of path, httpMethod and parameters. /// </summary> /// <param name="op"></param> /// <param name="httpMethod"></param> /// <param name="path"></param> /// <returns></returns> string ComposeActionNameWithPath(OpenApiOperation op, string httpMethod, string path) { string byWhat = String.Join("And", op.Parameters.Where(p => p.In == ParameterLocation.Query).Select(p => NameFunc.ToTitleCase(NameFunc.RefineParameterName(p.Name)))); return(PathToActionOrContainerName(path) + httpMethod + (String.IsNullOrEmpty(byWhat) ? String.Empty : "By" + byWhat)); }