public OpenApiSpecModel ExtractSpec(FileStream file,
                                            string overwriteApiTitle)
        {
            var output     = new OpenApiSpecModel();
            var serializer = new JsonSerializer();

            serializer.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

            JsonSpecModel jsonModel;

            using (var reader = new StreamReader(file)) {
                using (var jsonReader = new JsonTextReader(reader)) {
                    jsonModel = serializer.Deserialize <JsonSpecModel>(jsonReader);
                }
            }

            output.Info = jsonModel.Info;
            if (!string.IsNullOrEmpty(overwriteApiTitle))
            {
                output.Info.Title = overwriteApiTitle;
            }

            output.Definitions = new Dictionary <string, IOpenApiType>();
            if (jsonModel.Definitions != null)
            {
                foreach (var definedType in jsonModel.Definitions)
                {
                    output.Definitions[definedType.Key] = _typeParser.Parse(_objectParser,
                                                                            definedType.Value);
                }
            }

            if (jsonModel.Paths != null)
            {
                output.Endpoints = _endpointsParser.Parse(jsonModel.Paths);
                _shortNameService.CreateOrUpdateShortNames(output.Endpoints);
            }

            return(output);
        }
Ejemplo n.º 2
0
        public OpenApiSpecModel ExtractSpec(FileStream file,
                                            string overwriteApiTitle)
        {
            var output       = new OpenApiSpecModel();
            var deserializer = new DeserializerBuilder()
                               .WithNamingConvention(new CamelCaseNamingConvention())
                               .IgnoreUnmatchedProperties()
                               .Build();

            YamlSpecModel yamlModel;

            using (var reader = new StreamReader(file))
            {
                yamlModel = deserializer.Deserialize <YamlSpecModel>(reader);
            }

            output.Info = yamlModel.Info;
            if (!string.IsNullOrEmpty(overwriteApiTitle))
            {
                output.Info.Title = overwriteApiTitle;
            }

            output.Definitions = new Dictionary <string, IOpenApiType>();
            foreach (var definedType in yamlModel.Components.Schemas)
            {
                output.Definitions[definedType.Key] = _typeParser.Parse(_objectParser,
                                                                        definedType.Value);
            }

            if (yamlModel.Paths != null)
            {
                output.Endpoints = _endpointsParser.Parse(yamlModel.Paths);
                _shortNameService.CreateOrUpdateShortNames(output.Endpoints);
            }

            return(output);
        }