예제 #1
0
        /// <summary>
        /// Parses Swagger (OpenAPI) 2.0 into a service definition.
        /// </summary>
        public ServiceInfo ParseDefinition(NamedText source)
        {
            if (string.IsNullOrWhiteSpace(source.Text))
            {
                throw new ServiceDefinitionException("Service definition is missing.", new NamedTextPosition(source.Name, 1, 1));
            }

            SwaggerService       swaggerService;
            SwaggerParserContext context;

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

                        var exceptionPosition = new NamedTextPosition(source.Name, exception.End.Line, exception.End.Column);
                        throw new ServiceDefinitionException(exceptionError, exceptionPosition);
                    }
                }
                if (swaggerService == null)
                {
                    throw new ServiceDefinitionException("Service definition is missing.", new NamedTextPosition(source.Name, 1, 1));
                }

                context = SwaggerParserContext.FromYaml(source);
            }
            else
            {
                // parse JSON
                using (var stringReader = new StringReader(source.Text))
                    using (var jsonTextReader = new JsonTextReader(stringReader))
                    {
                        try
                        {
                            swaggerService = JsonSerializer.Create(SwaggerUtility.JsonSerializerSettings).Deserialize <SwaggerService>(jsonTextReader);
                        }
                        catch (JsonException exception)
                        {
                            var exceptionPosition = new NamedTextPosition(source.Name, jsonTextReader.LineNumber, jsonTextReader.LinePosition);
                            throw new ServiceDefinitionException(exception.Message, exceptionPosition);
                        }

                        context = SwaggerParserContext.FromJson(source);
                    }
            }

            return(ConvertSwaggerService(swaggerService, context));
        }
예제 #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);
        }