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)); } }
public static IReference LoadReference(OpenApiReference pointer, RootNode rootNode) { IReference referencedObject = null; var node = rootNode.Find(pointer.GetLocalPointer()); if (node == null && pointer.ReferenceType != ReferenceType.Tags) { return(null); } switch (pointer.ReferenceType) { case ReferenceType.Schema: referencedObject = OpenApiV3.LoadSchema(node); break; case ReferenceType.Parameter: referencedObject = OpenApiV3.LoadParameter(node); break; case ReferenceType.Callback: referencedObject = OpenApiV3.LoadCallback(node); break; case ReferenceType.SecurityScheme: referencedObject = OpenApiV3.LoadSecurityScheme(node); break; case ReferenceType.Link: referencedObject = OpenApiV3.LoadLink(node); break; case ReferenceType.Example: referencedObject = OpenApiV3.LoadExample(node); break; case ReferenceType.Tags: ListNode list = (ListNode)node; if (list != null) { foreach (var item in list) { var tag = OpenApiV3.LoadTag(item); if (tag.Name == pointer.TypeName) { return(tag); } } } else { return(new Tag() { Name = pointer.TypeName }); } break; default: throw new DomainParseException($"Unknown type of $ref {pointer.ReferenceType} at {pointer.ToString()}"); } return(referencedObject); }