Ejemplo n.º 1
0
        public static IReference LoadReference(OpenApiReference reference, RootNode rootNode)
        {
            IReference referencedObject = null;
            ParseNode  node             = rootNode.Find(GetPointer(reference));

            if (node == null && reference.ReferenceType != ReferenceType.Tags)
            {
                return(null);
            }
            switch (reference.ReferenceType)
            {
            case ReferenceType.Schema:
                referencedObject = OpenApiV2Reader.LoadSchema(node);
                break;

            case ReferenceType.Parameter:
                referencedObject = OpenApiV2Reader.LoadParameter(node);
                break;

            case ReferenceType.SecurityScheme:
                referencedObject = OpenApiV2Reader.LoadSecurityScheme(node);
                break;

            case ReferenceType.Tags:
                ListNode list = (ListNode)node;
                if (list != null)
                {
                    foreach (var item in list)
                    {
                        var tag = OpenApiV2Reader.LoadTag(item);

                        if (tag.Name == reference.TypeName)
                        {
                            return(tag);
                        }
                    }
                }
                else
                {
                    return(new Tag()
                    {
                        Name = reference.TypeName
                    });
                }
                break;

            default:
                throw new DomainParseException($"Unknown $ref {reference.ReferenceType} at {reference.ToString()}");
            }

            return(referencedObject);
        }
Ejemplo n.º 2
0
        public static ParsingContext Parse(Stream stream)
        {
            RootNode rootNode;
            var      context = new ParsingContext();

            try
            {
                var yamlStream = new YamlStream();
                yamlStream.Load(new StreamReader(stream));

                var yamlDocument = yamlStream.Documents.First();
                rootNode = new RootNode(context, yamlDocument);
            }
            catch (SharpYaml.SyntaxErrorException ex)
            {
                context.ParseErrors.Add(new OpenApiError("", ex.Message));
                context.OpenApiDocument = new OpenApiDocument();  // Could leave this null?
                return(context);
            }

            var inputVersion = GetVersion(rootNode);

            switch (inputVersion)
            {
            case "2.0":
                context.SetReferenceService(new ReferenceService(rootNode)
                {
                    loadReference  = OpenApiV2Reader.LoadReference,
                    parseReference = p => OpenApiV2Reader.ParseReference(p)
                });
                context.OpenApiDocument = OpenApiV2Reader.LoadOpenApi(rootNode);
                break;

            default:
                context.SetReferenceService(new ReferenceService(rootNode)
                {
                    loadReference  = OpenApiV3Reader.LoadReference,
                    parseReference = p => new OpenApiReference(p)
                });
                context.OpenApiDocument = OpenApiV3Reader.LoadOpenApi(rootNode);
                break;
            }
            return(context);
        }