public void Given_Type_When_Visit_Invoked_Then_It_Should_Return_Result(string dataType, string dataFormat)
        {
            var name     = "hello";
            var acceptor = new OpenApiSchemaAcceptor();
            var type     = new KeyValuePair <string, Type>(name, typeof(decimal));

            this._visitor.Visit(acceptor, type, this._strategy);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be(dataType);
            acceptor.Schemas[name].Format.Should().Be(dataFormat);
        }
Esempio n. 2
0
        public void Given_OpenApiSchemaVisibilityAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(string name, OpenApiVisibilityType visibility)
        {
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, typeof(decimal));
            var attribute = new OpenApiSchemaVisibilityAttribute(visibility);

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas[name].Extensions.Should().ContainKey("x-ms-visibility");
            acceptor.Schemas[name].Extensions["x-ms-visibility"].Should().BeOfType <OpenApiString>();
            (acceptor.Schemas[name].Extensions["x-ms-visibility"] as OpenApiString).Value.Should().Be(visibility.ToDisplayName(this._strategy));
        }
        public void Given_DataTypeAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(DataType dataType, string expected)
        {
            var name      = "hello";
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, typeof(DateTime));
            var attribute = new DataTypeAttribute(dataType);

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be("string");
            acceptor.Schemas[name].Format.Should().Be(expected);
        }
Esempio n. 4
0
        public void Given_MinLengthAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(int length)
        {
            var name      = "hello";
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, typeof(decimal));
            var attribute = new MinLengthAttribute(length);

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be("number");
            acceptor.Schemas[name].MinLength.Should().Be(length);
        }
Esempio n. 5
0
        public void Given_MaxLengthAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(Type listType, int length)
        {
            var name      = "hello";
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, listType);
            var attribute = new MaxLengthAttribute(length);

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be("array");
            acceptor.Schemas[name].MaxItems.Should().Be(length);
        }
Esempio n. 6
0
        public void Given_RegularExpressionAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(string pattern)
        {
            var name      = "hello";
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, typeof(DateTime));
            var attribute = new RegularExpressionAttribute(pattern);

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be("string");
            acceptor.Schemas[name].Pattern.Should().Be(pattern);
        }
Esempio n. 7
0
        public void Given_RangeAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(int min, int max)
        {
            var name      = "hello";
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, typeof(ulong));
            var attribute = new RangeAttribute(min, max);

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be("integer");
            acceptor.Schemas[name].Minimum.Should().Be(min);
            acceptor.Schemas[name].Maximum.Should().Be(max);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenApiHttpTrigger"/> class.
        /// </summary>
        public OpenApiHttpTriggerContext()
        {
            var host = HostJsonResolver.Resolve();

            this.OpenApiInfo  = OpenApiInfoResolver.Resolve(host);
            this.HttpSettings = host.GetHttpSettings();

            var filter   = new RouteConstraintFilter();
            var acceptor = new OpenApiSchemaAcceptor();
            var helper   = new DocumentHelper(filter, acceptor);

            this.Document  = new Document(helper);
            this.SwaggerUI = new SwaggerUI();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenApiHttpTrigger"/> class.
        /// </summary>
        public OpenApiHttpTriggerContext()
        {
            var host = HostJsonResolver.Resolve();

            this.OpenApiConfiguration = OpenApiConfigurationResolver.Resolve(this.GetExecutingAssembly());
            this.HttpSettings         = host.GetHttpSettings();

            var filter   = new RouteConstraintFilter();
            var acceptor = new OpenApiSchemaAcceptor();
            var helper   = new DocumentHelper(filter, acceptor);

            this.Document  = new Document(helper);
            this.SwaggerUI = new SwaggerUI();
        }
Esempio n. 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenApiTriggerFunctionProvider"/> class.
        /// </summary>
        public OpenApiHttpTriggerContext()
        {
            this.PackageAssembly = this.GetAssembly <ISwaggerUI>();

            var host = HostJsonResolver.Resolve();

            this.HttpSettings = host.GetHttpSettings();

            var filter   = new RouteConstraintFilter();
            var acceptor = new OpenApiSchemaAcceptor();
            var helper   = new DocumentHelper(filter, acceptor);

            this.Document  = new Document(helper);
            this.SwaggerUI = new SwaggerUI();
        }
        public void Given_OpenApiPropertyAttribute_Without_Default_When_Visit_Invoked_Then_It_Should_Return_Result(string name, bool nullable, string description)
        {
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, typeof(DateTime));
            var attribute = new OpenApiPropertyAttribute()
            {
                Nullable = nullable, Description = description
            };

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas[name].Nullable.Should().Be(nullable);
            acceptor.Schemas[name].Default.Should().BeNull();
            acceptor.Schemas[name].Description.Should().Be(description);
        }
        public void Given_Alias_Type_When_Visit_Invoked_Then_It_Should_Return_All_Sub_Schemas(Type type, params Type[] schemaTypes)
        {
            var acceptor = new OpenApiSchemaAcceptor();
            var key      = type.GetOpenApiReferenceId(type.IsOpenApiDictionary(), type.IsOpenApiArray(), this._strategy);

            this._visitor.Visit(acceptor, new KeyValuePair <string, Type>(key, type), this._strategy);

            acceptor.RootSchemas.Count.Should().Be(schemaTypes.Length);

            foreach (var schemaType in schemaTypes)
            {
                var subKey = schemaType.GetOpenApiReferenceId(schemaType.IsOpenApiDictionary(), schemaType.IsOpenApiArray(), this._strategy);

                acceptor.RootSchemas.Should().ContainKey(subKey);
            }
        }
Esempio n. 13
0
        public void Given_StringLengthAttribute_When_Visit_Invoked_Then_It_Should_Return_Result(int min, int max)
        {
            var name      = "hello";
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, typeof(DateTime));
            var attribute = new StringLengthAttribute(max)
            {
                MinimumLength = min
            };

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be("string");
            acceptor.Schemas[name].MinLength.Should().Be(min);
            acceptor.Schemas[name].MaxLength.Should().Be(max);
        }
        public void Given_OpenApiPropertyAttribute_With_Default_When_Visit_Invoked_Then_It_Should_Return_Result(string name, bool nullable, string description)
        {
            var @default  = FakeShortEnum.ShortValue1;
            var acceptor  = new OpenApiSchemaAcceptor();
            var type      = new KeyValuePair <string, Type>(name, typeof(FakeShortEnum));
            var attribute = new OpenApiPropertyAttribute()
            {
                Nullable = nullable, Default = @default, Description = description
            };

            this._visitor.Visit(acceptor, type, this._strategy, attribute);

            acceptor.Schemas[name].Nullable.Should().Be(nullable);
            acceptor.Schemas[name].Default.Should().NotBeNull();
            (acceptor.Schemas[name].Default as OpenApiInteger).Value.Should().Be((short)@default);
            acceptor.Schemas[name].Description.Should().Be(description);
        }
Esempio n. 15
0
        public void Given_Type_When_Visit_Invoked_Then_It_Should_Return_Result(Type objectType, string dataType, string dataFormat, int requiredCount, int rootSchemaCount, string referenceId)
        {
            var name     = "hello";
            var acceptor = new OpenApiSchemaAcceptor();
            var type     = new KeyValuePair <string, Type>(name, objectType);

            this._visitor.Visit(acceptor, type, this._strategy);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be(dataType);
            acceptor.Schemas[name].Format.Should().Be(dataFormat);

            acceptor.Schemas[name].Required.Count.Should().Be(requiredCount);

            acceptor.RootSchemas.Count.Should().Be(rootSchemaCount);

            acceptor.Schemas[name].Reference.Type.Should().Be(ReferenceType.Schema);
            acceptor.Schemas[name].Reference.Id.Should().Be(referenceId);
        }
        public void Given_Type_When_Visit_Invoked_Then_It_Should_Return_Result(string dataType, string dataFormat, Type enumType)
        {
            var name     = "hello";
            var acceptor = new OpenApiSchemaAcceptor();
            var type     = new KeyValuePair <string, Type>(name, typeof(FakeShortEnum));
            var enums    = enumType.ToOpenApiInt16Collection();

            this._visitor.Visit(acceptor, type, this._strategy);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be(dataType);
            acceptor.Schemas[name].Format.Should().Be(dataFormat);

            for (var i = 0; i < acceptor.Schemas[name].Enum.Count; i++)
            {
                var @enum = acceptor.Schemas[name].Enum[i];
                @enum.Should().BeOfType <OpenApiInteger>();
                (@enum as OpenApiInteger).Value.Should().Be((enums[i] as OpenApiInteger).Value);
            }

            (acceptor.Schemas[name].Default as OpenApiInteger).Value.Should().Be((enums.First() as OpenApiInteger).Value);
        }
        public void Given_Type_When_Visit_Invoked_Then_It_Should_Return_Result(Type dictionaryType, string dataType, string dataFormat, string additionalPropertyType, bool isReferential, string referenceId, int expected)
        {
            var name     = "hello";
            var acceptor = new OpenApiSchemaAcceptor();
            var type     = new KeyValuePair <string, Type>(name, dictionaryType);

            this._visitor.Visit(acceptor, type, this._strategy);

            acceptor.Schemas.Should().ContainKey(name);
            acceptor.Schemas[name].Type.Should().Be(dataType);
            acceptor.Schemas[name].Format.Should().Be(dataFormat);

            acceptor.Schemas[name].AdditionalProperties.Should().NotBeNull();
            acceptor.Schemas[name].AdditionalProperties.Type.Should().Be(additionalPropertyType);

            if (isReferential)
            {
                acceptor.Schemas[name].AdditionalProperties.Reference.Type.Should().Be(ReferenceType.Schema);
                acceptor.Schemas[name].AdditionalProperties.Reference.Id.Should().Be(referenceId);
            }

            acceptor.RootSchemas.Count(p => p.Key == referenceId).Should().Be(expected);
        }
Esempio n. 18
0
        /// <summary>
        /// Generates the OpenAPI document.
        /// </summary>
        /// <param name="project">Project path.</param>
        /// <param name="configuration">Copile configuration.</param>
        /// <param name="version">OpenAPI version.</param>
        /// <param name="format">OpenAPI output format.</param>
        /// <param name="output">Output path.</param>
        /// <param name="console">Value indicating whether to render the document on console or not.</param>
        public void Generate(
            [Option('p', Description = "Project path. Default is current directory")] string project  = ".",
            [Option('c', Description = "Configuration. Default is 'Debug'")] string configuration     = "Debug",
            [Option('t', Description = "Target framework. Default is 'netcoreapp2.1'")] string target = "netcoreapp2.1",
            [Option('v', Description = "OpenAPI spec version. Value can be either 'v2' or 'v3'. Default is 'v2'")] OpenApiVersionType version      = OpenApiVersionType.V2,
            [Option('f', Description = "OpenAPI output format. Value can be either 'json' or 'yaml'. Default is 'yaml'")] OpenApiFormatType format = OpenApiFormatType.Json,
            [Option('o', Description = "Generated OpenAPI output location. Default is 'output'")] string output = "output",
            bool console = false)
        {
            var pi = default(ProjectInfo);

            try
            {
                pi = new ProjectInfo(project, configuration, target);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);

                return;
            }

            var req = new Mock <HttpRequest>();

            req.SetupGet(p => p.Scheme).Returns("http");
            req.SetupGet(p => p.Host).Returns(new HostString("localhost", 7071));

            var filter         = new RouteConstraintFilter();
            var acceptor       = new OpenApiSchemaAcceptor();
            var namingStrategy = new CamelCaseNamingStrategy();
            var collection     = VisitorCollection.CreateInstance();
            var helper         = new DocumentHelper(filter, acceptor);
            var document       = new Document(helper);

            var swagger = default(string);

            try
            {
                swagger = document.InitialiseDocument()
                          .AddMetadata(pi.OpenApiInfo)
                          .AddServer(req.Object, pi.HostJsonHttpSettings.RoutePrefix)
                          .AddNamingStrategy(namingStrategy)
                          .AddVisitors(collection)
                          .Build(pi.CompiledDllPath)
                          .RenderAsync(version.ToOpenApiSpecVersion(), format.ToOpenApiFormat())
                          .Result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            if (console)
            {
                Console.WriteLine(swagger);
            }

            var outputpath = Path.IsPathFullyQualified(output)
                                 ? output
                                 : $"{pi.CompiledPath}{directorySeparator}{output}";

            if (!Directory.Exists(outputpath))
            {
                Directory.CreateDirectory(outputpath);
            }

            File.WriteAllText($"{outputpath}{directorySeparator}swagger.{format.ToDisplayName()}", swagger, Encoding.UTF8);
        }