private void ResolveReferences(OpenApiDiagnostic diagnostic, OpenApiDocument document)
        {
            // Resolve References if requested
            switch (_settings.ReferenceResolution)
            {
            case ReferenceResolutionSetting.ResolveAllReferences:
                throw new ArgumentException("Cannot resolve all references via a synchronous call. Use ReadAsync.");

            case ReferenceResolutionSetting.ResolveLocalReferences:
                var errors = document.ResolveReferences(false);

                foreach (var item in errors)
                {
                    diagnostic.Errors.Add(item);
                }
                break;

            case ReferenceResolutionSetting.DoNotResolveReferences:
                break;
            }
        }
        private async Task ResolveReferencesAsync(OpenApiDiagnostic diagnostic, OpenApiDocument document)
        {
            List <OpenApiError> errors = new List <OpenApiError>();

            // Resolve References if requested
            switch (_settings.ReferenceResolution)
            {
            case ReferenceResolutionSetting.ResolveAllReferences:

                // Create workspace for all documents to live in.
                var openApiWorkSpace = new OpenApiWorkspace();

                // Load this root document into the workspace
                var streamLoader    = new DefaultStreamLoader();
                var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, _settings.CustomExternalLoader ?? streamLoader, _settings);
                await workspaceLoader.LoadAsync(new OpenApiReference()
                {
                    ExternalResource = "/"
                }, document);

                // Resolve all references in all the documents loaded into the OpenApiWorkspace
                foreach (var doc in openApiWorkSpace.Documents)
                {
                    errors.AddRange(doc.ResolveReferences(true));
                }
                break;

            case ReferenceResolutionSetting.ResolveLocalReferences:
                errors.AddRange(document.ResolveReferences(false));
                break;

            case ReferenceResolutionSetting.DoNotResolveReferences:
                break;
            }

            foreach (var item in errors)
            {
                diagnostic.Errors.Add(item);
            }
        }
        public void OpenApiWorkspacesAllowDocumentsToReferenceEachOther_short()
        {
            var workspace = new OpenApiWorkspace();

            var doc = new OpenApiDocument();

            doc.CreatePathItem("/", p =>
            {
                p.Description = "Consumer";
                p.CreateOperation(OperationType.Get, op =>
                                  op.CreateResponse("200", re =>
                {
                    re.Description = "Success";
                    re.CreateContent("application/json", co =>
                                     co.Schema = new OpenApiSchema()
                    {
                        Reference = new OpenApiReference()        // Reference
                        {
                            Id               = "test",
                            Type             = ReferenceType.Schema,
                            ExternalResource = "common"
                        },
                        UnresolvedReference = true
                    }
                                     );
                })
                                  );
            });

            workspace.AddDocument("root", doc);
            workspace.AddDocument("common", CreateCommonDocument());
            var errors = doc.ResolveReferences(true);

            Assert.Empty(errors);

            var schema = doc.Paths["/"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema;

            Assert.False(schema.UnresolvedReference);
        }