Exemple #1
0
        public void AddExpectedResponse(string rawResponse, CodeBlockAnnotation annotation)
        {
            if (this.ExpectedResponse != null)
            {
                throw new InvalidOperationException("An expected response was already added to this request.");
            }

            this.ExpectedResponse         = rawResponse;
            this.ExpectedResponseMetadata = annotation;
        }
 public static MethodDefinition FromRequest(string request, CodeBlockAnnotation annotation, DocFile source)
 {
     var method = new MethodDefinition
     {
         Request = request,
         RequestMetadata = annotation,
         Identifier = annotation.MethodName,
         SourceFile = source
     };
     method.Title = method.Identifier;
     return method;
 }
        public static MethodDefinition FromRequest(string request, CodeBlockAnnotation annotation, DocFile source)
        {
            var method = new MethodDefinition
            {
                Request         = request,
                RequestMetadata = annotation,
                Identifier      = annotation.MethodName,
                SourceFile      = source
            };

            method.Title = method.Identifier;
            return(method);
        }
Exemple #4
0
        public static MethodDefinition FromRequest(string request, CodeBlockAnnotation annotation, DocFile source)
        {
            var method = new MethodDefinition
            {
                Request         = request,
                RequestMetadata = annotation,
                Identifier      = annotation.MethodName?.FirstOrDefault(),
                SourceFile      = source,
                RequiredScopes  = annotation.RequiredScopes
            };

            method.Title = method.Identifier;
            return(method);
        }
        public ExampleDefinition(CodeBlockAnnotation annotation, string content, DocFile source, string language)
        {
            if (string.IsNullOrEmpty(language))
            {
                throw new ArgumentNullException("language");
            }

            this.Metadata      = annotation;
            this.SourceExample = content;
            this.SourceFile    = source;

            switch (language.ToLower())
            {
            case "json":
            {
                this.Language = CodeLanguage.Json;
                try
                {
                    object inputObject = JsonConvert.DeserializeObject(content);
                    this.ParsedExample = JsonConvert.SerializeObject(inputObject, Formatting.Indented);
                }
                catch (Exception ex)
                {
                    Logging.LogMessage(
                        new ValidationError(
                            ValidationErrorCode.JsonParserException,
                            source.DisplayName,
                            "Error parsing resource definition: {0}",
                            ex.Message));
                }
                break;
            }

            case "http":
                this.ParsedExample = this.SourceExample;
                this.Language      = CodeLanguage.Http;
                break;

            default:
                this.Language = CodeLanguage.Unsupported;
                Logging.LogMessage(
                    new ValidationError(
                        ValidationErrorCode.UnsupportedLanguage,
                        source.DisplayName,
                        "The code language for this example is unuspported: {0}", language));
                break;
            }
        }
Exemple #6
0
        protected ResourceDefinition(CodeBlockAnnotation sourceAnnotation, string content, DocFile source, string language)
        {
            this.sourceAnnotation    = sourceAnnotation;
            this.OriginalExampleText = content;
            this.SourceFile          = source;
            this.Name            = sourceAnnotation.ResourceType;
            this.KeyPropertyName = sourceAnnotation.KeyPropertyName;

            if (string.IsNullOrEmpty(sourceAnnotation.ResourceType))
            {
                Logging.LogMessage(
                    new ValidationError(
                        ValidationErrorCode.MissingResourceName,
                        source.DisplayName,
                        "Resource definition is missing Name value"));
            }
        }
        public ExampleDefinition(CodeBlockAnnotation annotation, string content, DocFile source, string language)
        {
            if (string.IsNullOrEmpty(language))
                throw new ArgumentNullException("language");

            this.Metadata = annotation;
            this.SourceExample = content;
            this.SourceFile = source;

            switch (language.ToLower())
            {
                case "json":
                    {
                        this.Language = CodeLanguage.Json;
                        try
                        {
                            object inputObject = JsonConvert.DeserializeObject(content);
                            this.ParsedExample = JsonConvert.SerializeObject(inputObject, Formatting.Indented);
                        }
                        catch (Exception ex)
                        {
                            Logging.LogMessage(
                                new ValidationError(
                                    ValidationErrorCode.JsonParserException,
                                    source.DisplayName,
                                    "Error parsing resource definition: {0}",
                                    ex.Message));
                        }
                        break;
                    }
                case "http":
                    this.ParsedExample = this.SourceExample;
                    this.Language = CodeLanguage.Http;
                    break;
                default:
                    this.Language = CodeLanguage.Unsupported;
                    Logging.LogMessage(
                        new ValidationError(
                            ValidationErrorCode.UnsupportedLanguage,
                            source.DisplayName,
                            "The code language for this example is unuspported: {0}", language));
                    break;
            }
        }
        public ResourceDefinition(CodeBlockAnnotation annotation, string content, DocFile source, string language)
        {
            if (null != language && !language.Equals("json", StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("Resources only support JSON language.", "language");
            }

            this.Metadata        = annotation;
            this.OriginalExample = content;
            this.SourceFile      = source;

            try
            {
                object inputObject = JsonConvert.DeserializeObject(content);
                this.JsonExample = JsonConvert.SerializeObject(inputObject, Formatting.Indented);
            }
            catch (Exception ex)
            {
                Logging.LogMessage(
                    new ValidationError(
                        ValidationErrorCode.JsonParserException,
                        source.DisplayName,
                        "Error parsing resource definition: {0}",
                        ex.Message));
                throw;
            }

            if (string.IsNullOrEmpty(annotation.ResourceType))
            {
                Logging.LogMessage(
                    new ValidationError(
                        ValidationErrorCode.MissingResourceName,
                        source.DisplayName,
                        "Resource definition is missing a @odata.type name"));
            }
        }
        public ResourceDefinition(CodeBlockAnnotation annotation, string content, DocFile source, string language)
        {
            if (null != language && !language.Equals("json", StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("Resources only support JSON language.", "language");
            }

            this.Metadata = annotation;
            this.OriginalExample = content;
            this.SourceFile = source;

            try
            {
                object inputObject = JsonConvert.DeserializeObject(content);
                this.JsonExample = JsonConvert.SerializeObject(inputObject, Formatting.Indented);
            }
            catch (Exception ex)
            {
                Logging.LogMessage(
                    new ValidationError(
                        ValidationErrorCode.JsonParserException,
                        source.DisplayName,
                        "Error parsing resource definition: {0}",
                        ex.Message));
                throw;
            }

            if (string.IsNullOrEmpty(annotation.ResourceType))
            {
                Logging.LogMessage(
                    new ValidationError(
                        ValidationErrorCode.MissingResourceName,
                        source.DisplayName,
                        "Resource definition is missing a @odata.type name"));
            }
        }
Exemple #10
0
        /// <summary>
        /// Convert an annotation and fenced code block in the documentation into something usable. Adds
        /// the detected object into one of the internal collections of resources, methods, or examples.
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="code"></param>
        public ItemDefinition ParseCodeBlock(Block metadata, Block code)
        {
            if (metadata.BlockType != BlockType.html)
            {
                throw new ArgumentException("metadata block does not appear to be metadata");
            }

            if (code.BlockType != BlockType.codeblock)
            {
                throw new ArgumentException("code block does not appear to be code");
            }


            var metadataJsonString         = StripHtmlCommentTags(metadata.Content);
            CodeBlockAnnotation annotation = CodeBlockAnnotation.ParseMetadata(metadataJsonString, code);

            switch (annotation.BlockType)
            {
            case CodeBlockType.Resource:
            {
                ResourceDefinition resource;
                if (code.CodeLanguage.Equals("json", StringComparison.OrdinalIgnoreCase))
                {
                    resource = new JsonResourceDefinition(annotation, code.Content, this);
                }
                //else if (code.CodeLanguage.Equals("xml", StringComparison.OrdinalIgnoreCase))
                //{
                //
                //}
                else
                {
                    throw new NotSupportedException("Unsupported resource definition language: " + code.CodeLanguage);
                }

                if (string.IsNullOrEmpty(resource.Name))
                {
                    throw new InvalidDataException("Resource definition is missing a name");
                }

                this.resources.Add(resource);
                return(resource);
            }

            case CodeBlockType.Request:
            {
                var method = MethodDefinition.FromRequest(code.Content, annotation, this);
                if (string.IsNullOrEmpty(method.Identifier))
                {
                    method.Identifier = string.Format("{0} #{1}", this.DisplayName, this.requests.Count);
                }
                this.requests.Add(method);
                return(method);
            }

            case CodeBlockType.Response:
            {
                MethodDefinition pairedRequest = null;
                if (!string.IsNullOrEmpty(annotation.MethodName))
                {
                    // Look up paired request by name
                    pairedRequest = (from m in this.requests where m.Identifier == annotation.MethodName select m).FirstOrDefault();
                }
                else if (this.requests.Any())
                {
                    pairedRequest = Enumerable.Last(this.requests);
                }

                if (null == pairedRequest)
                {
                    throw new InvalidOperationException(string.Format("Unable to locate the corresponding request for response block: {0}. Requests must be defined before a response.", annotation.MethodName));
                }

                pairedRequest.AddExpectedResponse(code.Content, annotation);
                return(pairedRequest);
            }

            case CodeBlockType.Example:
            {
                var example = new ExampleDefinition(annotation, code.Content, this, code.CodeLanguage);
                this.examples.Add(example);
                return(example);
            }

            case CodeBlockType.Ignored:
                return(null);

            case CodeBlockType.SimulatedResponse:
            {
                var method = Enumerable.Last(this.requests);
                method.AddSimulatedResponse(code.Content, annotation);
                return(method);
            }

            case CodeBlockType.TestParams:
            {
                var method = Enumerable.Last(this.requests);
                method.AddTestParams(code.Content);
                return(method);
            }

            default:
            {
                var errorMessage = string.Format("Unable to parse metadata block or unsupported block type. Line {1}. Content: {0}", metadata.Content, metadata.LineStart);
                throw new NotSupportedException(errorMessage);
            }
            }
        }
 public void AddSimulatedResponse(string rawResponse, CodeBlockAnnotation annotation)
 {
     this.ActualResponse = rawResponse;
 }
        public void AddExpectedResponse(string rawResponse, CodeBlockAnnotation annotation)
        {
            if (this.ExpectedResponse != null)
            {
                throw new InvalidOperationException("An expected response was already added to this request.");
            }

            this.ExpectedResponse = rawResponse;
            this.ExpectedResponseMetadata = annotation;
        }
Exemple #13
0
 public JsonResourceDefinition(CodeBlockAnnotation sourceAnnotation, string json, DocFile source)
     : base(sourceAnnotation, json, source, "json")
 {
     ParseJsonInput();
 }
Exemple #14
0
        /// <summary>
        /// Convert an annotation and fenced code block in the documentation into something usable. Adds
        /// the detected object into one of the internal collections of resources, methods, or examples.
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="code"></param>
        public ItemDefinition ParseCodeBlock(Block metadata, Block code)
        {
            if (metadata.BlockType != BlockType.html)
            {
                throw new ArgumentException("metadata block does not appear to be metadata");
            }

            if (code.BlockType != BlockType.codeblock)
            {
                throw new ArgumentException("code block does not appear to be code");
            }

            var metadataJsonString = metadata.Content.Substring(4, metadata.Content.Length - 9);
            var annotation         = CodeBlockAnnotation.FromJson(metadataJsonString);

            switch (annotation.BlockType)
            {
            case CodeBlockType.Resource:
            {
                var resource = new ResourceDefinition(annotation, code.Content, this, code.CodeLanguage);
                this.resources.Add(resource);
                return(resource);
            }

            case CodeBlockType.Request:
            {
                var method = MethodDefinition.FromRequest(code.Content, annotation, this);
                if (string.IsNullOrEmpty(method.Identifier))
                {
                    method.Identifier = string.Format("{0} #{1}", this.DisplayName, this.requests.Count);
                }
                this.requests.Add(method);
                return(method);
            }

            case CodeBlockType.Response:
            {
                MethodDefinition pairedRequest = null;
                if (!string.IsNullOrEmpty(annotation.MethodName))
                {
                    // Look up paired request by name
                    pairedRequest = (from m in this.requests where m.Identifier == annotation.MethodName select m).FirstOrDefault();
                }
                else
                {
                    pairedRequest = Enumerable.Last(this.requests);
                }

                if (null == pairedRequest)
                {
                    throw new InvalidOperationException(string.Format("Unable to locate the corresponding request for response block: {0}. Requests must be defined before a response.", annotation.MethodName));
                }

                pairedRequest.AddExpectedResponse(code.Content, annotation);
                return(pairedRequest);
            }

            case CodeBlockType.Example:
            {
                var example = new ExampleDefinition(annotation, code.Content, this, code.CodeLanguage);
                this.examples.Add(example);
                return(example);
            }

            case CodeBlockType.Ignored:
                return(null);

            case CodeBlockType.SimulatedResponse:
            {
                var method = Enumerable.Last(this.requests);
                method.AddSimulatedResponse(code.Content, annotation);
                return(method);
            }

            case CodeBlockType.TestParams:
            {
                var method = Enumerable.Last(this.requests);
                method.AddTestParams(code.Content);
                return(method);
            }

            default:
                throw new NotSupportedException("Unsupported block type: " + annotation.BlockType);
            }
        }
Exemple #15
0
 public void AddSimulatedResponse(string rawResponse, CodeBlockAnnotation annotation)
 {
     this.ActualResponse = rawResponse;
 }
        protected ResourceDefinition(CodeBlockAnnotation sourceAnnotation, string content, DocFile source, string language)
        {
            this.sourceAnnotation = sourceAnnotation;
            this.OriginalExampleText = content;
            this.SourceFile = source;
            this.Name = sourceAnnotation.ResourceType;
            this.KeyPropertyName = sourceAnnotation.KeyPropertyName;

            if (string.IsNullOrEmpty(sourceAnnotation.ResourceType))
            {
                Logging.LogMessage(
                    new ValidationError(
                        ValidationErrorCode.MissingResourceName,
                        source.DisplayName,
                        "Resource definition is missing Name value"));
            }
        }
 public JsonResourceDefinition(CodeBlockAnnotation sourceAnnotation, string json, DocFile source)
     : base(sourceAnnotation, json, source, "json")
 {
     ParseJsonInput();
 }