示例#1
0
        private OpenApiParameter GenerateSampleParameter(ParameterDescriptor apiParameter,
                                                         SchemaRepository schemaRespository)
        {
            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameter.Name.ToCamelCase()
                : apiParameter.Name;

            var location   = ParameterLocationMap[apiParameter.From];
            var isRequired = (apiParameter.From == ParameterFrom.Path) ||
                             apiParameter.Type.GetCustomAttributes()
                             .Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var schema = GenerateSchema(apiParameter.Type, schemaRespository, null,
                                        apiParameter.ParameterInfo);
            var parameter = new OpenApiParameter
            {
                Name     = name,
                In       = location,
                Required = isRequired,
                Schema   = schema
            };

            var filterContext = new ParameterFilterContext(
                apiParameter,
                _schemaGenerator,
                schemaRespository,
                null,
                apiParameter.ParameterInfo);

            foreach (var filter in _options.ParameterFilters)
            {
                filter.Apply(parameter, filterContext);
            }

            return(parameter);
        }
示例#2
0
        public OpenApiDocument GetSwagger(string documentName, string host = null, string basePath = null)
        {
            if (!_options.SwaggerDocs.TryGetValue(documentName, out OpenApiInfo info))
            {
                throw new UnknownSwaggerDocument(documentName, _options.SwaggerDocs.Select(d => d.Key));
            }

            var schemaRepository = new SchemaRepository(documentName);
            var entries          = _serviceEntryManager.GetAllEntries();
            var swaggerDoc       = new OpenApiDocument
            {
                Info       = info,
                Servers    = GenerateServers(host, basePath),
                Paths      = GeneratePaths(entries, schemaRepository),
                Components = new OpenApiComponents
                {
                    Schemas         = schemaRepository.Schemas,
                    SecuritySchemes = new Dictionary <string, OpenApiSecurityScheme>(_options.SecuritySchemes)
                },
                SecurityRequirements = new List <OpenApiSecurityRequirement>(_options.SecurityRequirements)
            };

            return(swaggerDoc);
        }