Beispiel #1
0
        /// <summary>Generates the Swagger specification.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The Swagger specification.</returns>
        protected virtual async Task <string> GenerateSwaggerAsync(IOwinContext context)
        {
            if (_schemaException != null && _schemaTimestamp + _settings.ExceptionCacheTime > DateTimeOffset.UtcNow)
            {
                throw _schemaException;
            }

            if (_schemaJson == null)
            {
                try
                {
                    var settings  = _settings.CreateGeneratorSettings(null);
                    var generator = new WebApiToSwaggerGenerator(settings, _schemaGenerator);
                    var document  = await generator.GenerateForControllersAsync(_controllerTypes);

                    document.Host = context.Request.Host.Value ?? "";
                    document.Schemes.Add(context.Request.Scheme == "http" ? SwaggerSchema.Http : SwaggerSchema.Https);
                    document.BasePath = context.Request.PathBase.Value?.Substring(0, context.Request.PathBase.Value.Length - (_settings.MiddlewareBasePath?.Length ?? 0)) ?? "";

                    _settings.PostProcess?.Invoke(document);
                    _schemaJson      = document.ToJson();
                    _schemaException = null;
                    _schemaTimestamp = DateTimeOffset.UtcNow;
                }
                catch (Exception exception)
                {
                    _schemaJson      = null;
                    _schemaException = exception;
                    _schemaTimestamp = DateTimeOffset.UtcNow;
                    throw _schemaException;
                }
            }

            return(_schemaJson);
        }
Beispiel #2
0
        /// <summary>Generates the Swagger specification.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The Swagger specification.</returns>
        protected virtual async Task <string> GenerateDocumentAsync(IOwinContext context)
        {
            var settings  = _settings.CreateGeneratorSettings(null, null);
            var generator = new WebApiOpenApiDocumentGenerator(settings);
            var document  = await generator.GenerateForControllersAsync(_controllerTypes);

            if (_settings.MiddlewareBasePath != null)
            {
                document.Host = context.Request.Host.Value ?? "";
                document.Schemes.Add(context.Request.Scheme == "http" ? OpenApiSchema.Http : OpenApiSchema.Https);
                document.BasePath = context.Request.PathBase.Value?.Substring(0, context.Request.PathBase.Value.Length - (_settings.MiddlewareBasePath?.Length ?? 0)) ?? "";
            }
            else
            {
                document.Servers.Clear();
                document.Servers.Add(new OpenApiServer
                {
                    Url = context.Request.GetServerUrl()
                });
            }

            _settings.PostProcess?.Invoke(document);
            var schemaJson = document.ToJson();

            return(schemaJson);
        }
Beispiel #3
0
        /// <summary>Generates the Swagger specification.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The Swagger specification.</returns>
        protected virtual async Task <string> GenerateDocumentAsync(IOwinContext context)
        {
            var settings  = _settings.CreateGeneratorSettings(null, null);
            var generator = new WebApiToSwaggerGenerator(settings, _schemaGenerator);
            var document  = await generator.GenerateForControllersAsync(_controllerTypes);

            document.Host = context.Request.Host.Value ?? "";
            document.Schemes.Add(context.Request.Scheme == "http" ? SwaggerSchema.Http : SwaggerSchema.Https);
            document.BasePath = context.Request.PathBase.Value?.Substring(0, context.Request.PathBase.Value.Length - (_settings.MiddlewareBasePath?.Length ?? 0)) ?? "";

            _settings.PostProcess?.Invoke(document);
            var schemaJson = document.ToJson();

            return(schemaJson);
        }
        /// <summary>Generates the Swagger specification.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The Swagger specification.</returns>
        protected virtual async Task <string> GenerateSwaggerAsync(HttpContext context)
        {
            if (_schemaException != null && _schemaTimestamp + _settings.ExceptionCacheTime > DateTimeOffset.UtcNow)
            {
                throw _schemaException;
            }

            var apiDescriptionGroups = _apiDescriptionGroupCollectionProvider.ApiDescriptionGroups;

            if (apiDescriptionGroups.Version == Volatile.Read(ref _version) && _schemaJson != null)
            {
                return(_schemaJson);
            }

            try
            {
                var serializerSettings = _mvcJsonOptions.Value.SerializerSettings;
                var settings           = _settings.CreateGeneratorSettings(serializerSettings);
                var generator          = new AspNetCoreToSwaggerGenerator(settings, _schemaGenerator);
                var document           = await generator.GenerateAsync(apiDescriptionGroups);

                document.Host = context.Request.Host.Value ?? "";
                document.Schemes.Add(context.Request.Scheme == "http" ? SwaggerSchema.Http : SwaggerSchema.Https);
                document.BasePath = context.Request.PathBase.Value?.Substring(0, context.Request.PathBase.Value.Length - (_settings.MiddlewareBasePath?.Length ?? 0)) ?? "";

                _settings.PostProcess?.Invoke(document);
                _schemaJson      = document.ToJson();
                _schemaException = null;
                _version         = apiDescriptionGroups.Version;
                _schemaTimestamp = DateTimeOffset.UtcNow;
            }
            catch (Exception exception)
            {
                _schemaJson      = null;
                _schemaException = exception;
                _schemaTimestamp = DateTimeOffset.UtcNow;
                throw _schemaException;
            }

            return(_schemaJson);
        }
Beispiel #5
0
        /// <summary>Generates the Swagger specification.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The Swagger specification.</returns>
        protected virtual async Task <string> GenerateDocumentAsync(IOwinContext context)
        {
            var settings  = _settings.CreateGeneratorSettings(null, null);
            var generator = new WebApiOpenApiDocumentGenerator(settings);
            var document  = await generator.GenerateForControllersAsync(_controllerTypes);

            if (_settings.MiddlewareBasePath != null)
            {
                document.Host = context.Request.Host.Value ?? "";
                document.Schemes.Add(context.Request.Scheme == "http" ? OpenApiSchema.Http : OpenApiSchema.Https);
                document.BasePath = context.Request.PathBase.Value?.Substring(0, context.Request.PathBase.Value.Length - (_settings.MiddlewareBasePath?.Length ?? 0)) ?? "";
            }

            // iiQ Custom
            //else
            //{
            //    document.Servers.Clear();
            //    document.Servers.Add(new OpenApiServer
            //    {
            //        Url = context.Request.GetServerUrl()
            //    });
            //}

            // iiQ Custom
            var updated = new Dictionary <string, OpenApiPathItem>();

            foreach (var kv in document.Paths)
            {
                updated.Add(kv.Key.Replace("/v1.0", ""), kv.Value);
            }
            document.Paths.Clear();
            foreach (var kv in updated)
            {
                document.Paths.Add(kv);
            }

            _settings.PostProcess?.Invoke(document);
            var schemaJson = document.ToJson();

            return(schemaJson);
        }