示例#1
0
        /// <summary>
        /// Initiates the parsing process.  Not thread safe and should only be called once on a parsing context
        /// </summary>
        /// <param name="yamlDocument">Yaml document to parse.</param>
        /// <param name="diagnostic">Diagnostic object which will return diagnostic results of the operation.</param>
        /// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
        internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diagnostic)
        {
            RootNode = new RootNode(this, diagnostic, yamlDocument);

            var inputVersion = GetVersion(RootNode);

            OpenApiDocument doc;

            switch (inputVersion)
            {
            case string version when version == "2.0":
                VersionService = new OpenApiV2VersionService();
                doc            = VersionService.LoadDocument(RootNode);
                diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
                break;

            case string version when version.StartsWith("3.0"):
                VersionService = new OpenApiV3VersionService();

                doc = VersionService.LoadDocument(RootNode);
                diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
                break;

            default:
                throw new OpenApiUnsupportedSpecVersionException(inputVersion);
            }

            return(doc);
        }
示例#2
0
        /// <summary>
        /// Initiates the parsing process.  Not thread safe and should only be called once on a parsing context
        /// </summary>
        /// <param name="yamlDocument"></param>
        /// <param name="diagnostic"></param>
        /// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
        internal OpenApiDocument Parse(YamlDocument yamlDocument, OpenApiDiagnostic diagnostic)
        {
            RootNode = new RootNode(this, diagnostic, yamlDocument);

            var inputVersion = GetVersion(RootNode);

            OpenApiDocument doc;

            if (inputVersion == "2.0")
            {
                VersionService = new OpenApiV2VersionService();
                doc            = this.VersionService.LoadDocument(this.RootNode);
            }
            else if (inputVersion.StartsWith("3.0."))
            {
                this.VersionService = new OpenApiV3VersionService();
                doc = this.VersionService.LoadDocument(this.RootNode);
            }
            else
            {
                // If version number is not recognizable,
                // our best effort will try to deserialize the document to V3.
                this.VersionService = new OpenApiV3VersionService();
                doc = this.VersionService.LoadDocument(this.RootNode);
            }
            return(doc);
        }
示例#3
0
        /// <summary>
        /// Gets the referenced object.
        /// </summary>
        public IOpenApiReferenceable GetReferencedObject(
            OpenApiDiagnostic diagnostic,
            ReferenceType referenceType,
            string referenceString)
        {
            _referenceStore.TryGetValue(referenceString, out var referencedObject);

            // If reference has already been accessed once, simply return the same reference object.
            if (referencedObject != null)
            {
                return(referencedObject);
            }

            var reference = VersionService.ConvertToOpenApiReference(referenceString, referenceType);

            var isReferencedObjectFound = VersionService.TryLoadReference(this, reference, out referencedObject);

            if (isReferencedObjectFound)
            {
                // Populate the Reference section of the object, so that the writers
                // can recognize that this is referencing another object.
                referencedObject.Reference = reference;
                _referenceStore.Add(referenceString, referencedObject);
            }
            else if (referencedObject != null)
            {
                return(referencedObject);
            }
            else
            {
                diagnostic.Errors.Add(
                    new OpenApiError(
                        GetLocation(),
                        $"Cannot resolve the reference {referenceString}"));
            }

            return(referencedObject);
        }
示例#4
0
        /// <summary>
        /// Initiates the parsing process of a fragment.  Not thread safe and should only be called once on a parsing context
        /// </summary>
        /// <param name="yamlDocument"></param>
        /// <param name="version">OpenAPI version of the fragment</param>
        /// <param name="diagnostic">Diagnostic object which will return diagnostic results of the operation.</param>
        /// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
        internal T ParseFragment <T>(YamlDocument yamlDocument, OpenApiSpecVersion version, OpenApiDiagnostic diagnostic) where T : IOpenApiElement
        {
            var node = ParseNode.Create(this, diagnostic, yamlDocument.RootNode);

            T element = default(T);

            switch (version)
            {
            case OpenApiSpecVersion.OpenApi2_0:
                VersionService = new OpenApiV2VersionService();
                element        = this.VersionService.LoadElement <T>(node);
                break;

            case OpenApiSpecVersion.OpenApi3_0:
                this.VersionService = new OpenApiV3VersionService();
                element             = this.VersionService.LoadElement <T>(node);
                break;
            }

            return(element);
        }
示例#5
0
 /// <summary>
 /// Create Parsing Context
 /// </summary>
 /// <param name="diagnostic">Provide instance for diagnotic object for collecting and accessing information about the parsing.</param>
 public ParsingContext(OpenApiDiagnostic diagnostic)
 {
     Diagnostic = diagnostic;
 }