Esempio n. 1
0
        private static void AddPathItem(IDictionary <string, OpenApiPathItem> items, IEnumerable <CommandRequestRegistration> registrations, DocumentProcessorContext context)
        {
            var item = new OpenApiPathItem();

            foreach (var registration in registrations)
            {
                var method    = registration.RequestMethod.ToLower();
                var operation = new OpenApiOperation
                {
                    Description = registration.OpenApiDescription ?? (registration.CommandType ?? typeof(object)).Name,
                    Summary     = registration.OpenApiSummary + (registration.IsQueued ? " (queued)" : string.Empty),
                    OperationId = HashAlgorithm.ComputeHash($"{method} {registration.Route}"),
                    Tags        = new[] { !registration.OpenApiGroupName.IsNullOrEmpty() ? $"{registration.OpenApiGroupPrefix} ({registration.OpenApiGroupName})" : registration.OpenApiGroupPrefix }.ToList(),
                    Produces = registration.OpenApiProduces.Safe(ContentType.JSON.ToValue()).Split(';').Distinct().ToList(),
                    //RequestBody = new OpenApiRequestBody{}
                };

                item.Add(method, operation);

                var hasResponseModel = registration.ResponseType?.Name.SafeEquals("object") == false && !registration.IsQueued;
                var description      = registration.OpenApiResponseDescription ?? (hasResponseModel ? registration.ResponseType : null)?.FullPrettyName();
                var jsonSchema       = context.SchemaGenerator.Generate(registration.ResponseType, context.SchemaResolver);
                // reuse some previously generated schemas, so schema $refs are avoided
                if (!description.IsNullOrEmpty())
                {
                    if (Schemas.ContainsKey(description))
                    {
                        jsonSchema = Schemas[description];
                    }
                    else
                    {
                        Schemas.Add(description, jsonSchema);
                    }
                }

                operation.Responses.Add(((int)registration.OnSuccessStatusCode).ToString(), new OpenApiResponse
                {
                    Description = description,
                    Schema      = hasResponseModel ? jsonSchema : null,
                    //Examples = hasResponseModel ? Factory.Create(registration.ResponseType) : null // header?
                });
                operation.Responses.Add(((int)HttpStatusCode.BadRequest).ToString(), new OpenApiResponse
                {
                    Description = string.Empty,
                });
                operation.Responses.Add(((int)HttpStatusCode.InternalServerError).ToString(), new OpenApiResponse
                {
                    Description = string.Empty
                });

                AddResponseHeaders(operation, registration);
                AddOperationParameters(operation, method, registration, context);
            }

            if (item.Any() && !items.ContainsKey(registrations.First().Route))
            {
                items?.Add(registrations.First().Route, item);
            }
        }
Esempio n. 2
0
        private OpenApiPathItem PathFor(Type type, List <string> httpMethod)
        {
            var openApiPathItem = new OpenApiPathItem();

            httpMethod.ForEach(method => openApiPathItem.Add(
                                   new KeyValuePair <string, OpenApiOperation>(method, OperationFor(method, type))));
            return(openApiPathItem);
        }
Esempio n. 3
0
        private static void AddPathItem(IDictionary <string, OpenApiPathItem> items, IEnumerable <CommandEndpointRegistration> registrations, DocumentProcessorContext context)
        {
            var item = new OpenApiPathItem();

            foreach (var registration in registrations)
            {
                var method    = registration.Method.ToString().ToLower();
                var operation = new NSwag.OpenApiOperation
                {
                    Description = registration.OpenApi.Description,
                    Summary     = registration.OpenApi.Summary,
                    OperationId = registration.RequestType.Name, //GetStringSha256Hash($"{method} {registration.Pattern}"),
                    Tags        = new[] { !registration.OpenApi.GroupName.IsNullOrEmpty() ? $"{registration.OpenApi.GroupPrefix} {registration.OpenApi.GroupName}".Trim() : registration.OpenApi.GroupPrefix }.ToList(),
                    Produces = !registration.OpenApi.Produces.IsNullOrEmpty() ? registration.OpenApi.Produces.Split(';').Distinct().ToList() : new[] { "application/json" }.ToList()
                    //RequestBody = new OpenApiRequestBody{}
                };

                item.Add(method, operation);

                var hasResponseModel = registration.Response?.IgnoreResponseBody == false && registration.ResponseType != typeof(Unit) && registration.ResponseType?.Name.SafeEquals("object") == false;
                var description      = hasResponseModel ? registration.ResponseType.PrettyName() : string.Empty;
                var schema           = context.SchemaGenerator.Generate(registration.ResponseType, context.SchemaResolver);
                var schemaKey        = registration.ResponseType.PrettyName();

                // reuse some previously generated schemas, so schema $refs are avoided
                if (ResponseSchemas.ContainsKey(schemaKey))
                {
                    schema = ResponseSchemas[schemaKey];
                }
                else
                {
                    ResponseSchemas.Add(schemaKey, schema);
                }

                if (registration.Response == null)
                {
                    operation.Responses.Add(((int)HttpStatusCode.OK).ToString(), new OpenApiResponse
                    {
                        Description = description,
                        Schema      = hasResponseModel ? schema : null,
                        //Examples = hasResponseModel ? Factory.Create(registration.ResponseType) : null // header?
                    });
                }
                else
                {
                    operation.Responses.Add(((int)registration.Response.OnSuccessStatusCode).ToString(), new OpenApiResponse
                    {
                        Description = description,
                        Schema      = hasResponseModel ? schema : null,
                        //Examples = hasResponseModel ? Factory.Create(registration.ResponseType) : null // header?
                    });
                }

                operation.Responses.Add(((int)HttpStatusCode.BadRequest).ToString(), new OpenApiResponse
                {
                    Description = nameof(ProblemDetails),
                    Schema      = ResponseSchemas[nameof(ProblemDetails)]
                });
                operation.Responses.Add(((int)HttpStatusCode.InternalServerError).ToString(), new OpenApiResponse
                {
                    Description = nameof(ProblemDetails),
                    Schema      = ResponseSchemas[nameof(ProblemDetails)]
                });

                AddResponseHeaders(operation, registration);
                AddOperationParameters(operation, method, registration, context);
            }

            if (item.Any() && !items.ContainsKey(registrations.First().Pattern))
            {
                items?.Add(registrations.First().Pattern, item);
            }
        }