/// <summary>
        /// Converts the input RAML stream to an Open API Specification document.
        /// </summary>
        /// <param name="inputPath">The path to the RAML file.</param>
        public OpenApiDocument ConvertToOpenApiDocument(string inputPath)
        {
            var builder = new DeserializerBuilder();

            var includeNodeDeserializer = new YamlIncludeNodeDeserializer(new YamlIncludeNodeDeserializerOptions
            {
                DirectoryName = Path.GetDirectoryName(inputPath),
                Deserializer  = builder.Build()
            });

            _deserializer = builder
                            .WithTagMapping(Constants.IncludeTag, typeof(IncludeRef))
                            .WithNodeDeserializer(includeNodeDeserializer, s => s.OnTop())
                            .Build();

            var result = _deserializer.Deserialize <Dictionary <object, object> >(File.ReadAllText(inputPath));

            // Step 1 - Get all types and schemas
            var types = result.GetAsDictionary("types");

            if (types != null)
            {
                foreach (var type in types.Where(x => !_types.ContainsKey(x.Key)))
                {
                    _types.Add(type.Key, type.Value);
                }
            }
            var schemas = result.GetAsDictionary("schemas");

            if (schemas != null)
            {
                foreach (var schema in schemas.Where(x => !_types.ContainsKey(x.Key)))
                {
                    _types.Add(schema.Key, schema.Value);
                }
            }

            // Step 2 - Get Info, Servers and Components
            _doc = new OpenApiDocument
            {
                Info       = MapInfo(result),
                Servers    = MapServers(result),
                Components = MapComponents(_types)
            };

            // Step 3 - Get Paths
            _doc.Paths = MapPaths(result);

            // Check if valid
            var text = _doc.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);

            return(_doc);
        }
Example #2
0
        public static IDeserializer Build(string directoryName)
        {
            var builder = new DeserializerBuilder();

            var includeNodeDeserializerOptions = new YamlIncludeNodeDeserializerOptions
            {
                DirectoryName = directoryName
            };

            var includeNodeDeserializer = new YamlIncludeNodeDeserializer(includeNodeDeserializerOptions);

            return(builder
                   .WithTagMapping(string.Empty, typeof(IncludeRef))
                   .WithTagMapping(Constants.IncludeTag, typeof(IncludeRef))
                   .WithNodeDeserializer(includeNodeDeserializer, s => s.OnTop())
                   .Build());
        }