public IList Apply()

in src/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration/OperationFilters/ParamToRequestBodyFilter.cs [41:184]


        public IList<GenerationError> Apply(
            OpenApiOperation operation,
            XElement element,
            OperationFilterSettings settings)
        {
            var generationErrors = new List<GenerationError>();

            try
            {
                var bodyElements = element.Elements()
                    .Where(
                        p => p.Name == KnownXmlStrings.Param &&
                            p.Attribute(KnownXmlStrings.In)?.Value == KnownXmlStrings.Body)
                    .ToList();

                var generationContext = settings.GenerationContext;

                foreach (var bodyElement in bodyElements)
                {
                    var name = bodyElement.Attribute(KnownXmlStrings.Name)?.Value.Trim();
                    var mediaType = bodyElement.Attribute(KnownXmlStrings.Type)?.Value ?? "application/json";

                    var description = bodyElement.GetDescriptionTextFromLastTextNode();

                    var allListedTypes = bodyElement.GetListedTypes();

                    if (!allListedTypes.Any())
                    {
                        throw new InvalidRequestBodyException(
                            string.Format(SpecificationGenerationMessages.MissingSeeCrefTag, name));
                    }

                    var crefKey = allListedTypes.ToCrefKey();

                    OpenApiSchema schema = new OpenApiSchema();
                    if (generationContext.CrefToSchemaMap.ContainsKey(crefKey))
                    {
                        var schemaInfo = generationContext.CrefToSchemaMap[crefKey];

                        if (schemaInfo.Error != null)
                        {
                            generationErrors.Add(schemaInfo.Error);

                            return generationErrors;
                        }

                        schemaInfo.Schema.CopyInto(schema);
                    }

                    var examples = bodyElement.ToOpenApiExamples(
                        generationContext.CrefToFieldValueMap,
                        generationErrors);

                    var schemaReferenceDefaultVariant = generationContext
                        .VariantSchemaReferenceMap[DocumentVariantInfo.Default];

                    if (examples.Count > 0)
                    {
                        var firstExample = examples.First().Value?.Value;

                        if (firstExample != null)
                        {
                            // In case a schema is a reference, find that schema object in schema registry
                            // and update the example.
                            if (schema.Reference != null)
                            {
                                if (schemaReferenceDefaultVariant.ContainsKey(schema.Reference.Id))
                                {
                                    schemaReferenceDefaultVariant[schema.Reference.Id].Example = firstExample;
                                }
                            }
                            else
                            {
                                schema.Example = firstExample;
                            }
                        }
                    }

                    if (operation.RequestBody == null)
                    {
                        operation.RequestBody = new OpenApiRequestBody
                        {
                            Description = description.RemoveBlankLines(),
                            Content =
                        {
                            [mediaType] = new OpenApiMediaType {Schema = schema}
                        },
                            Required = true
                        };
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(operation.RequestBody.Description))
                        {
                            operation.RequestBody.Description = description.RemoveBlankLines();
                        }

                        if (!operation.RequestBody.Content.ContainsKey(mediaType))
                        {
                            operation.RequestBody.Content[mediaType] = new OpenApiMediaType
                            {
                                Schema = schema
                            };
                        }
                        else
                        {
                            if (!operation.RequestBody.Content[mediaType].Schema.AnyOf.Any())
                            {
                                var existingSchema = operation.RequestBody.Content[mediaType].Schema;
                                var newSchema = new OpenApiSchema();
                                newSchema.AnyOf.Add(existingSchema);

                                operation.RequestBody.Content[mediaType].Schema = newSchema;
                            }

                            operation.RequestBody.Content[mediaType].Schema.AnyOf.Add(schema);
                        }
                    }

                    if (examples.Count > 0)
                    {
                        if (operation.RequestBody.Content[mediaType].Examples.Any())
                        {
                            examples.CopyInto(operation.RequestBody.Content[mediaType].Examples);
                        }
                        else
                        {
                            operation.RequestBody.Content[mediaType].Examples = examples;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                generationErrors.Add(
                   new GenerationError
                   {
                       Message = ex.Message,
                       ExceptionType = ex.GetType().Name
                   });
            }

            return generationErrors;
        }