コード例 #1
0
        /// <summary>Generates missing or non-unique operation IDs.</summary>
        public void GenerateOperationIds()
        {
            // TODO: Improve this method

            // Generate missing IDs
            foreach (var operation in Operations.Where(o => string.IsNullOrEmpty(o.Operation.OperationId)))
            {
                operation.Operation.OperationId = GetOperationNameFromPath(operation);
            }

            // Find non-unique operation IDs
            foreach (var group in Operations.GroupBy(o => o.Operation.OperationId))
            {
                var operations = group.ToList();
                if (group.Count() > 1)
                {
                    // Append "All" if possible
                    var arrayResponseOperation = operations.FirstOrDefault(
                        o => o.Operation.ActualResponses.Any(r =>
                                                             HttpUtilities.IsSuccessStatusCode(r.Key) &&
                                                             r.Value.Schema?.ActualSchema.Type == JsonObjectType.Array));

                    if (arrayResponseOperation != null)
                    {
                        var name = arrayResponseOperation.Operation.OperationId + "All";
                        if (Operations.All(o => o.Operation.OperationId != name))
                        {
                            arrayResponseOperation.Operation.OperationId = name;
                            operations.Remove(arrayResponseOperation);
                            GenerateOperationIds();
                            return;
                        }
                    }

                    // Add numbers
                    var i = 2;
                    foreach (var operation in operations.Skip(1))
                    {
                        operation.Operation.OperationId += i++;
                    }

                    GenerateOperationIds();
                    return;
                }
            }
        }
コード例 #2
0
ファイル: OpenApiDocument.cs プロジェクト: domestos99/NSwag
        /// <summary>Generates missing or non-unique operation IDs.</summary>
        public void GenerateOperationIds()
        {
            // Generate missing IDs
            foreach (var operation in Operations.Where(o => string.IsNullOrEmpty(o.Operation.OperationId)))
            {
                operation.Operation.OperationId = GetOperationNameFromPath(operation);
            }

            // Find non-unique operation IDs

            // 1: Append all to methods returning collections
            foreach (var group in Operations.GroupBy(o => o.Operation.OperationId))
            {
                if (group.Count() > 1)
                {
                    var collections = group.Where(o => o.Operation.ActualResponses.Any(r =>
                                                                                       HttpUtilities.IsSuccessStatusCode(r.Key) &&
                                                                                       r.Value.Schema?.ActualSchema.Type == JsonObjectType.Array));
                    // if we have just collections, adding All will not help in discrimination
                    if (collections.Count() == group.Count())
                    {
                        continue;
                    }

                    foreach (var o in group)
                    {
                        var isCollection = o.Operation.ActualResponses.Any(r =>
                                                                           HttpUtilities.IsSuccessStatusCode(r.Key) &&
                                                                           r.Value.Schema?.ActualSchema.Type == JsonObjectType.Array);

                        if (isCollection)
                        {
                            o.Operation.OperationId += "All";
                        }
                    }
                }
            }

            // 2: Append the Method type
            foreach (var group in Operations.GroupBy(o => o.Operation.OperationId))
            {
                if (group.Count() > 1)
                {
                    var methods = group.Select(o => o.Method.ToUpper()).Distinct();
                    if (methods.Count() == 1)
                    {
                        continue;
                    }

                    foreach (var o in group)
                    {
                        o.Operation.OperationId += o.Method.ToUpper();
                    }
                }
            }

            // 3: Append numbers as last resort
            foreach (var group in Operations.GroupBy(o => o.Operation.OperationId))
            {
                var operations = group.ToList();
                if (group.Count() > 1)
                {
                    // Add numbers
                    var i = 2;
                    foreach (var operation in operations.Skip(1))
                    {
                        operation.Operation.OperationId += i++;
                    }

                    GenerateOperationIds();
                    return;
                }
            }
        }