public static SwaggerConversion Create(SwaggerService swaggerService, string?serviceName, SwaggerParserContext context)
        {
            var conversion = new SwaggerConversion(swaggerService, serviceName);

            conversion.Convert(context);
            return(conversion);
        }
Пример #2
0
        /// <summary>
        /// Implements TryParseDefinition.
        /// </summary>
        protected override bool TryParseDefinitionCore(ServiceDefinitionText text, out ServiceInfo?service, out IReadOnlyList <ServiceDefinitionError> errors)
        {
            var isFsd = new FsdParser().TryParseDefinition(text, out service, out errors);

            if (isFsd || text.Name.EndsWith(".fsd", StringComparison.OrdinalIgnoreCase))
            {
                return(isFsd);
            }

            service = null;

            if (string.IsNullOrWhiteSpace(text.Text))
            {
                errors = new[] { new ServiceDefinitionError("Service definition is missing.", new ServiceDefinitionPosition(text.Name, 1, 1)) };
                return(false);
            }

            SwaggerService       swaggerService;
            SwaggerParserContext context;

            if (!s_detectJsonRegex.IsMatch(text.Text))
            {
                // parse YAML
                var yamlDeserializer = new DeserializerBuilder()
                                       .IgnoreUnmatchedProperties()
                                       .WithNamingConvention(new OurNamingConvention())
                                       .Build();
                using (var stringReader = new StringReader(text.Text))
                {
                    try
                    {
                        swaggerService = yamlDeserializer.Deserialize <SwaggerService>(stringReader);
                    }
                    catch (YamlException exception)
                    {
                        var          errorMessage    = exception.InnerException?.Message ?? exception.Message;
                        const string errorStart      = "): ";
                        var          errorStartIndex = errorMessage.IndexOf(errorStart, StringComparison.OrdinalIgnoreCase);
                        if (errorStartIndex != -1)
                        {
                            errorMessage = errorMessage.Substring(errorStartIndex + errorStart.Length);
                        }

                        errors = new[] { new ServiceDefinitionError(errorMessage, new ServiceDefinitionPosition(text.Name, exception.End.Line, exception.End.Column)) };
                        return(false);
                    }
                }

                if (swaggerService == null)
                {
                    errors = new[] { new ServiceDefinitionError("Service definition is missing.", new ServiceDefinitionPosition(text.Name, 1, 1)) };
                    return(false);
                }

                context = SwaggerParserContext.FromYaml(text);
            }
            else
            {
                // parse JSON
                using (var stringReader = new StringReader(text.Text))
                    using (var jsonTextReader = new JsonTextReader(stringReader))
                    {
                        try
                        {
                            swaggerService = JsonSerializer.Create(SwaggerUtility.JsonSerializerSettings).Deserialize <SwaggerService>(jsonTextReader);
                        }
                        catch (JsonException exception)
                        {
                            errors = new[] { new ServiceDefinitionError(exception.Message, new ServiceDefinitionPosition(text.Name, jsonTextReader.LineNumber, jsonTextReader.LinePosition)) };
                            return(false);
                        }

                        context = SwaggerParserContext.FromJson(text);
                    }
            }

            var conversion = SwaggerConversion.Create(swaggerService, ServiceName, context);

            service = conversion.Service;
            errors  = conversion.Errors;
            return(errors.Count == 0);
        }