示例#1
0
        public async Task When_yaml_OpenAPI_spec_has_external_schema_refs_they_are_resolved(string relativePath, string docPath, string header)
        {
            var path = GetTestDirectory() + relativePath;

            //// Act
            OpenApiDocument doc = await OpenApiYamlDocument.FromFileAsync(path);

            IDictionary <string, OpenApiPathItem> docPaths = doc.Paths;
            OpenApiPathItem  pathItem  = docPaths[docPath];
            OpenApiOperation operation = pathItem["get"];
            IDictionary <string, OpenApiResponse> responses = operation.Responses;

            OpenApiResponse OK        = responses["200"].ActualResponse;
            OpenApiHeaders  OKheaders = OK.Headers;

            OpenApiResponse Unauthorized = responses["401"].ActualResponse;

            ////Assert

            // Header schema loaded correctly from headers.yaml
            Assert.True(OKheaders.ContainsKey(header));
            Assert.NotNull(OKheaders[header]);

            //Response data loaded correctly from responses.yaml
            string problemType = "application/problem+json";

            Assert.True(Unauthorized.Content.ContainsKey(problemType));
            Assert.NotNull(Unauthorized.Content[problemType]);
            Assert.NotNull(Unauthorized.Schema);
        }
示例#2
0
        public async Task When_yaml_OpenAPI_spec_has__relative_external_schema_refs_in_subdirs__they_are_resolved(string relativePath)
        {
            var path = GetTestDirectory() + relativePath;

            OpenApiDocument doc = await OpenApiYamlDocument.FromFileAsync(path);

            IDictionary <string, OpenApiPathItem> docPaths = doc.Paths;

            OpenApiPathItem  pathItem  = docPaths["/life-cycles"];
            OpenApiOperation operation = pathItem["get"];
            IDictionary <string, OpenApiResponse> responses = operation.Responses;
            OpenApiResponse    OK              = responses["200"].ActualResponse;
            var                schema          = OK.Content["application/json"];
            JsonSchemaProperty items           = schema.Schema.ActualSchema.ActualProperties["items"];
            var                innerProperties = items.Item.ActualSchema.ActualProperties;

            string[] expectedProperties = new string[] { "id", "systemName", "name", "smallImageID", "helpText" };

            foreach (string property in expectedProperties)
            {
                Assert.True(innerProperties.ContainsKey(property));
            }

            pathItem  = docPaths["/ad-hoc-tasks/{adhocTaskId}/execute"];
            operation = pathItem["post"];
            responses = operation.Responses;
            OK        = responses["200"].ActualResponse;
            schema    = OK.Content["application/json"];

            Assert.Equal("status", schema.Schema.ActualDiscriminator);
            Assert.Equal("Completed", schema.Schema.ActualDiscriminatorObject.Mapping.Keys.First());
            Assert.Equal(2, schema.Schema.ActualSchema.ActualProperties["status"].ActualSchema.Enumeration.Count);
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="specification"></param>
        /// <returns></returns>
        public NSwagRunner FromYamlSpecification(FilePath specification)
        {
            string          path     = Context.MakeAbsolute(specification).FullPath;
            OpenApiDocument document = OpenApiYamlDocument.FromFileAsync(path).Result;

            return(new NSwagRunner(Context, document));
        }
 public OpenApiDocument GetDocument(string swaggerFile)
 {
     return(swaggerFile.EndsWith("yaml") || swaggerFile.EndsWith("yml")
         ? OpenApiYamlDocument.FromFileAsync(swaggerFile)
            .GetAwaiter()
            .GetResult()
         : OpenApiDocument.FromFileAsync(swaggerFile)
            .GetAwaiter()
            .GetResult());
 }
示例#5
0
        public async Task When_yaml_schema_has_references_it_works(string relativePath)
        {
            //// Arrange
            var path = GetTestDirectory() + relativePath;

            //// Act
            var document = await OpenApiYamlDocument.FromFileAsync(path);

            var json = document.ToJson();

            //// Assert
            Assert.Equal(JsonObjectType.Integer, document.Definitions["ContractObject"].Properties["foo"].ActualTypeSchema.Type);
            Assert.Equal(JsonObjectType.Boolean, document.Definitions["ContractObject"].Properties["bar"].ActualTypeSchema.Type);
        }
 public async Task <OpenApiDocument> GetDocumentAsync(string swaggerFile)
 {
     try
     {
         return(await ThreadHelper.JoinableTaskFactory.RunAsync(
                    () => swaggerFile.EndsWith("yaml") || swaggerFile.EndsWith("yml")
                    ?OpenApiYamlDocument.FromFileAsync(swaggerFile)
                    : OpenApiDocument.FromFileAsync(swaggerFile)));
     }
     catch (NullReferenceException)
     {
         return(await(swaggerFile.EndsWith("yaml") || swaggerFile.EndsWith("yml")
                 ? OpenApiYamlDocument.FromFileAsync(swaggerFile)
                 : OpenApiDocument.FromFileAsync(swaggerFile)));
     }
 }
 public OpenApiDocument GetDocument(string swaggerFile)
 {
     try
     {
         return(ThreadHelper.JoinableTaskFactory?.Run(
                    () => swaggerFile.EndsWith("yaml") || swaggerFile.EndsWith("yml")
                 ? OpenApiYamlDocument.FromFileAsync(swaggerFile)
                 : OpenApiDocument.FromFileAsync(swaggerFile)));
     }
     catch (NullReferenceException)
     {
         return((swaggerFile.EndsWith("yaml") || swaggerFile.EndsWith("yml")
                 ? OpenApiYamlDocument.FromFileAsync(swaggerFile)
                 : OpenApiDocument.FromFileAsync(swaggerFile))
                .GetAwaiter()
                .GetResult());
     }
 }
示例#8
0
 protected async Task <OpenApiDocument> ReadSwaggerDocumentAsync(string input)
 {
     if (!IsJson(input) && !IsYaml(input))
     {
         if (input.StartsWith("http://") || input.StartsWith("https://"))
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
         }
         else
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromFileAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromFileAsync(input).ConfigureAwait(false));
             }
         }
     }
     else
     {
         if (IsYaml(input))
         {
             return(await OpenApiYamlDocument.FromYamlAsync(input).ConfigureAwait(false));
         }
         else
         {
             return(await OpenApiDocument.FromJsonAsync(input).ConfigureAwait(false));
         }
     }
 }
示例#9
0
 internal static async Task <OpenApiDocument> ReadSwaggerDocumentAsync(this string input)
 {
     if (!input.IsJson() && !input.IsYaml())
     {
         if (input.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || input.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
         }
         else
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromFileAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromFileAsync(input).ConfigureAwait(false));
             }
         }
     }
     else
     {
         return(input.IsYaml()
             ? await OpenApiYamlDocument.FromYamlAsync(input).ConfigureAwait(false)
             : await OpenApiDocument.FromJsonAsync(input).ConfigureAwait(false));
     }
 }
示例#10
0
        private static async Task Main(string[] args)
        {
            string baseDir         = FindBaseDir();
            string sourcepath      = Path.Combine(baseDir, "api");
            string destinationpath = Path.Combine(baseDir, "BlockM3.AEternity.SDK", "Generated");

            string[] sources = Directory.GetFiles(sourcepath, "s*.yml");
            if (sources.Length != 2)
            {
                throw new ArgumentException("Expecting only 2 swagger definitions in api directory (api and compiler)");
            }


            CSharpClientGeneratorSettings apisettings = new CSharpClientGeneratorSettings
            {
                CSharpGeneratorSettings = { Namespace             = "BlockM3.AEternity.SDK.Generated.Api",
                                            SchemaType            = SchemaType.OpenApi3,
                                            PropertyNameGenerator = new CustomTypeScriptPropertyNameGenerator() }, AdditionalNamespaceUsages = new[] { "BlockM3.AEternity.SDK.Generated.Models", "System.Numerics" }, AdditionalContractNamespaceUsages = new[] { "System.Numerics" }
            };

            apisettings.CSharpGeneratorSettings.ValueGenerator = new CustomValueGenerator(apisettings.CSharpGeneratorSettings);

            CSharpClientGeneratorSettings compilersettings = new CSharpClientGeneratorSettings
            {
                CSharpGeneratorSettings = { ExcludedTypeNames = new[] { "ByteCode", "Error" }, Namespace = "BlockM3.AEternity.SDK.Generated.Compiler", SchemaType = SchemaType.OpenApi3, PropertyNameGenerator = new CustomTypeScriptPropertyNameGenerator() }, AdditionalNamespaceUsages = new[] { "BlockM3.AEternity.SDK.Generated.Models", "System.Numerics" }, AdditionalContractNamespaceUsages = new[] { "System.Numerics" }, GenerateExceptionClasses = false,
            };

            compilersettings.CSharpGeneratorSettings.ValueGenerator = new CustomValueGenerator(apisettings.CSharpGeneratorSettings);
            string compiler = sources.FirstOrDefault(a => a.Contains("compiler"));

            if (compiler == null)
            {
                throw new ArgumentException("Expecting compiler api definition in api");
            }
            string          api         = sources.First(a => a != compiler);
            OpenApiDocument apidocument = await OpenApiYamlDocument.FromFileAsync(api);

            OpenApiDocument compilerdocument = await OpenApiYamlDocument.FromFileAsync(compiler);

            var    apigenerator      = new CSharpClientGenerator(apidocument, apisettings, CreateResolverWithExceptionSchema(apisettings.CSharpGeneratorSettings, apidocument));
            var    compilergenerator = new CSharpClientGenerator(compilerdocument, compilersettings, CreateResolverWithExceptionSchema(compilersettings.CSharpGeneratorSettings, compilerdocument));
            string apiclient         = apigenerator.GenerateFile(ClientGeneratorOutputType.Implementation);

            apigenerator.Settings.CSharpGeneratorSettings.Namespace = "BlockM3.AEternity.SDK.Generated.Models";
            string apimodels      = apigenerator.GenerateFile(ClientGeneratorOutputType.Contracts);
            string compilerclient = compilergenerator.GenerateFile(ClientGeneratorOutputType.Implementation);

            compilergenerator.Settings.CSharpGeneratorSettings.Namespace = "BlockM3.AEternity.SDK.Generated.Models";
            string compilermodels = compilergenerator.GenerateFile(ClientGeneratorOutputType.Contracts);
            string apipath        = Path.Combine(destinationpath, "Api");
            string compilerpath   = Path.Combine(destinationpath, "Compiler");
            string modelpath      = Path.Combine(destinationpath, "Models");

            try
            {
                Directory.CreateDirectory(apipath);
            }
            catch
            {
                // ignored
            }

            try
            {
                Directory.CreateDirectory(compilerpath);
            }
            catch
            {
                //ignored
            }

            try
            {
                Directory.CreateDirectory(modelpath);
            }
            catch
            {
                //ignored
            }

            await File.WriteAllTextAsync(Path.Combine(apipath, "Client.cs"), apiclient, Encoding.UTF8);

            await File.WriteAllTextAsync(Path.Combine(compilerpath, "Client.cs"), compilerclient, Encoding.UTF8);

            await File.WriteAllTextAsync(Path.Combine(modelpath, "Models.cs"), apimodels, Encoding.UTF8);

            await File.WriteAllTextAsync(Path.Combine(modelpath, "ModelsCompiler.cs"), compilermodels, Encoding.UTF8);
        }
 public Task <OpenApiDocument> GetDocumentAsync(string swaggerFile)
 {
     return(swaggerFile.EndsWith("yaml") || swaggerFile.EndsWith("yml")
         ? OpenApiYamlDocument.FromFileAsync(swaggerFile)
         : OpenApiDocument.FromFileAsync(swaggerFile));
 }