Example #1
0
        public OpenApiDocument Parse(Stream stream)
        {
            this.context = new ParsingContext();
            try
            {
                this.rootNode = new RootNode(this.context, stream);
            }
            catch (SharpYaml.SyntaxErrorException ex)
            {
                ParseErrors.Add(new OpenApiError("", ex.Message));
                return(new OpenApiDocument());
            }

            inputVersion = GetVersion(this.rootNode);

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

            default:
                this.context.SetReferenceService(new ReferenceService(this.rootNode)
                {
                    loadReference  = OpenApiV3.LoadReference,
                    parseReference = p => new OpenApiReference(p)
                });
                return(OpenApiV3.LoadOpenApi(this.rootNode));
            }
        }
Example #2
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 = OpenApiV2.LoadSchema(node);
                break;

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

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

            case ReferenceType.Tags:
                ListNode list = (ListNode)node;
                if (list != null)
                {
                    foreach (var item in list)
                    {
                        var tag = OpenApiV2.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);
        }