Beispiel #1
0
        private void ValidateHeaders(
            IDictionary <string, OpenApiHeader> headerSpecs,
            OpenApiDocument openApiDocument,
            NameValueCollection headerValues)
        {
            foreach (var entry in headerSpecs)
            {
                var value      = headerValues[entry.Key];
                var headerSpec = entry.Value;

                if (headerSpec.Required && value == null)
                {
                    throw new ResponseDoesNotMatchSpecException($"Required header '{entry.Key}' is not present");
                }

                if (value == null || headerSpec.Schema == null)
                {
                    continue;
                }

                var schema = (headerSpec.Schema.Reference != null)
                    ? (OpenApiSchema)openApiDocument.ResolveReference(headerSpec.Schema.Reference)
                    : headerSpec.Schema;

                if (value == null)
                {
                    continue;
                }

                if (!schema.TryParse(value, out object typedValue))
                {
                    throw new ResponseDoesNotMatchSpecException($"Header '{entry.Key}' is not of type '{headerSpec.Schema.TypeIdentifier()}'");
                }
            }
        }
Beispiel #2
0
        public bool Validate(
            OpenApiSchema schema,
            OpenApiDocument openApiDocument,
            JToken instance,
            out IEnumerable <string> errorMessages)
        {
            schema = (schema.Reference != null)
                ? (OpenApiSchema)openApiDocument.ResolveReference(schema.Reference)
                : schema;

            var errorMessagesList = new List <string>();

            foreach (var subValidator in _subValidators)
            {
                if (!subValidator.CanValidate(schema))
                {
                    continue;
                }

                if (!subValidator.Validate(schema, openApiDocument, instance, out IEnumerable <string> subErrorMessages))
                {
                    errorMessagesList.AddRange(subErrorMessages);
                }
            }

            errorMessages = errorMessagesList;
            return(!errorMessages.Any());
        }
 private T ResolveReference <T>(OpenApiReference reference) where T : class, IOpenApiReferenceable, new()
 {
     if (string.IsNullOrEmpty(reference.ExternalResource))
     {
         try
         {
             return(_currentDocument.ResolveReference(reference) as T);
         }
         catch (OpenApiException ex)
         {
             _errors.Add(new OpenApiError(ex));
             return(null);
         }
     }
     else if (_resolveRemoteReferences == true)
     {
         // TODO: Resolve Remote reference (Targeted for 1.1 release)
         return(new T()
         {
             UnresolvedReference = true,
             Reference = reference
         });
     }
     else
     {
         // Leave as unresolved reference
         return(new T()
         {
             UnresolvedReference = true,
             Reference = reference
         });
     }
 }
Beispiel #4
0
        private void ValidateParameters(
            IEnumerable <OpenApiParameter> parameterSpecs,
            OpenApiDocument openApiDocument,
            NameValueCollection parameterNameValues)
        {
            foreach (var parameterSpec in parameterSpecs)
            {
                var value = parameterNameValues[parameterSpec.Name];

                if ((parameterSpec.In == ParameterLocation.Path || parameterSpec.Required) && value == null)
                {
                    throw new RequestDoesNotMatchSpecException($"Required parameter '{parameterSpec.Name}' is not present");
                }

                if (value == null || parameterSpec.Schema == null)
                {
                    continue;
                }

                var schema = (parameterSpec.Schema.Reference != null)
                    ? (OpenApiSchema)openApiDocument.ResolveReference(parameterSpec.Schema.Reference)
                    : parameterSpec.Schema;

                if (!schema.TryParse(value, out object typedValue))
                {
                    throw new RequestDoesNotMatchSpecException($"Parameter '{parameterSpec.Name}' is not of type '{parameterSpec.Schema.TypeIdentifier()}'");
                }
            }
        }
Beispiel #5
0
 private T ResolveReference <T>(OpenApiReference reference) where T : class, IOpenApiReferenceable, new()
 {
     if (string.IsNullOrEmpty(reference.ExternalResource))
     {
         return(_currentDocument.ResolveReference(reference) as T);
     }
     else if (_resolveRemoteReferences == true)
     {
         // TODO: Resolve Remote reference
         return(new T()
         {
             UnresolvedReference = true,
             Reference = reference
         });
     }
     else
     {
         // Leave as unresolved reference
         return(new T()
         {
             UnresolvedReference = true,
             Reference = reference
         });
     }
 }
Beispiel #6
0
 public void ShouldAllowComponentsThatJustContainAReference()
 {
     using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "ComponentRootReference.json")))
     {
         OpenApiStreamReader reader  = new OpenApiStreamReader();
         OpenApiDocument     doc     = reader.Read(stream, out OpenApiDiagnostic diags);
         OpenApiSchema       schema1 = doc.Components.Schemas["AllPets"];
         Assert.False(schema1.UnresolvedReference);
         OpenApiSchema schema2 = (OpenApiSchema)doc.ResolveReference(schema1.Reference);
         if (schema2.UnresolvedReference && schema1.Reference.Id == schema2.Reference.Id)
         {
             // detected a cycle - this code gets triggered
             Assert.True(false, "A cycle should not be detected");
         }
     }
 }
Beispiel #7
0
        private static IEnumerable <OpenApiParameter> ExpandParameterSpecs(
            OpenApiPathItem pathSpec,
            OpenApiOperation operationSpec,
            OpenApiDocument openApiDocument)
        {
            var securityParameterSpecs = DeriveSecurityParameterSpecs(operationSpec, openApiDocument);

            return(securityParameterSpecs
                   .Concat(pathSpec.Parameters)
                   .Concat(operationSpec.Parameters)
                   .Select(p =>
            {
                return (p.Reference != null)
                        ? (OpenApiParameter)openApiDocument.ResolveReference(p.Reference)
                        : p;
            }));
        }
        private T ResolveReference<T>(OpenApiReference reference) where T : class, IOpenApiReferenceable, new()
        {
            if (string.IsNullOrEmpty(reference.ExternalResource))
            {
                try
                {
                    return _currentDocument.ResolveReference(reference) as T;
                }
                catch (OpenApiException ex)
                {
                    _errors.Add(new OpenApiReferenceError(ex));
                    return null;
                }
            }
            else if (_resolveRemoteReferences == true)
            {
                if (_currentDocument.Workspace == null)
                {
                    _errors.Add(new OpenApiReferenceError(reference,"Cannot resolve external references for documents not in workspaces."));
                    // Leave as unresolved reference
                    return new T()
                    {
                        UnresolvedReference = true,
                        Reference = reference
                    };
                }
                var target = _currentDocument.Workspace.ResolveReference(reference);

                // TODO:  If it is a document fragment, then we should resolve it within the current context

                return target as T;
            }
            else
            {
                // Leave as unresolved reference
                return new T()
                {
                    UnresolvedReference = true,
                    Reference = reference
                };
            }
        }
Beispiel #9
0
        private void ValidateContent(OpenApiRequestBody requestBodySpec, OpenApiDocument openApiDocument, HttpContent content)
        {
            requestBodySpec = (requestBodySpec.Reference != null)
                ? (OpenApiRequestBody)openApiDocument.ResolveReference(requestBodySpec.Reference)
                : requestBodySpec;

            if (requestBodySpec.Required && content == null)
            {
                throw new RequestDoesNotMatchSpecException("Required content is not present");
            }

            if (content == null)
            {
                return;
            }

            if (!requestBodySpec.Content.TryGetValue(content.Headers.ContentType.MediaType, out OpenApiMediaType mediaTypeSpec))
            {
                throw new RequestDoesNotMatchSpecException($"Content media type '{content.Headers.ContentType.MediaType}' is not specified");
            }

            try
            {
                foreach (var contentValidator in _contentValidators)
                {
                    if (contentValidator.CanValidate(content.Headers.ContentType.MediaType))
                    {
                        contentValidator.Validate(mediaTypeSpec, openApiDocument, content);
                    }
                }
            }
            catch (ContentDoesNotMatchSpecException contentException)
            {
                throw new RequestDoesNotMatchSpecException($"Content does not match spec. {contentException.Message}");
            }
        }
 public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
 {
     return(_document.ResolveReference(reference));
 }
 public static ILocatedOpenApiElement <T> ResolveComponentReference <T>(this OpenApiDocument document,
                                                                        OpenApiReference reference)
     where T : IOpenApiElement =>
 ((T)document.ResolveReference(reference)).CreateRoot(reference.Id);