Beispiel #1
0
        private static ParsingNode ParseTree(ByteArrayReadStream stream)
        {
            // Skipping the first 4 bytes coz they are used to store a... identifier?
            stream.Skip(IdentifierLength);

            var firstMarker = (MarkupByte)stream.ReadByte();

            if (firstMarker != MarkupByte.Start)
            {
                throw new MalformedWorldException();
            }

            var guessedMaximumNodeDepth = 128;
            var nodeStack = new Stack <ParsingNode>(capacity: guessedMaximumNodeDepth);

            var rootNodeType = (byte)stream.ReadByte();
            var rootNode     = new ParsingNode()
            {
                Type      = (NodeType)rootNodeType,
                DataBegin = (int)stream.Position
            };

            nodeStack.Push(rootNode);

            ParseTreeAfterRootNodeStart(stream, nodeStack);
            if (nodeStack.Count != 0)
            {
                throw new MalformedWorldException();
            }

            return(rootNode);
        }
Beispiel #2
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiServerVariable;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "Enum":
                    element.Enum = (childNode.Value as ListParsingNode).CreateList(n => (string)(n as ValueParsingNode).Value);
                    break;

                case "default":
                    element.Default = childNode.GetSimpleValue <string>();
                    break;

                case "description":
                    element.Description = childNode.GetSimpleValue <string>();
                    break;
                }
            }
        }
Beispiel #3
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiTag;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "name":
                    element.Name = childNode.GetSimpleValue <string>();
                    break;

                case "description":
                    element.Description = childNode.GetSimpleValue <string>();
                    break;

                case "externalDocs":
                    element.ExternalDocs = childNode.Value.ParseIntoElement <OpenApiExternalDocs, OpenApiExternalDocsParsingStrategy>();
                    break;
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Creates an OpenAPI 3 RequestBody from a Swagger 2.0 body parameter.
        /// </summary>
        /// <param name="node"> Contains current parsing context - used for retrieving values from temporary storage. </param>
        /// <param name="bodyParam"> Body parameter to be converted. </param>
        /// <returns> Newly created RequestBody </returns>
        private static OpenApiRequestBody BodyParameterToRequestBody(ParsingNode node, OpenApiParameter bodyParam)
        {
            var requestBody = new OpenApiRequestBody();

            requestBody.Description = bodyParam.Description;
            requestBody.Required    = bodyParam.Required;
            requestBody.Extensions  = bodyParam.Extensions;
            if (bodyParam.Schema != null)
            {
                requestBody.Content = new Dictionary <string, OpenApiMediaType>();
                var consumes = node.storage.Retrieve <IList <string> >("operation.consumes");
                if (consumes == null)
                {
                    consumes = node.storage.Retrieve <IList <string> >("root.consumes");
                }
                if (consumes == null)
                {
                    consumes = new List <string> {
                        "application/json"
                    };
                }
                foreach (var mediaType in consumes)
                {
                    requestBody.Content.Add(mediaType, new OpenApiMediaType // maybe want to not replace keys?
                    {
                        Schema = bodyParam.Schema
                    });
                }
            }
            return(requestBody);
        }
Beispiel #5
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }

            var element = parsingElement as OpenApiDocument;

            var pathsNode = node.childNodes.Where(c => c.Name == "paths").First().Value;

            pathsNode.strategy = new OpenApiPathsPartialParsingStrategy(_pathName, _operationName);
            element.Paths      = new OpenApiPaths();
            pathsNode.Parse(element.Paths);

            // collect unresolved references
            _unresolvedReferences = new List <OpenApiReference>();
            var path      = element.Paths.First().Value;   // parsed only a single path
            var operation = path.Operations.First().Value; // parsed only a single operation

            CollectOperationReferences(operation);
            foreach (var parameter in path.Parameters)
            {
                CollectParameterReferences(parameter); // path parameters are referenceable
            }

            if (_unresolvedReferences.Count > 0)
            {
                // build components to contain only references from parsed pathItem/operation
                var componentsNode = node.childNodes.Where(c => c.Name == "components").First().Value;
                componentsNode.strategy = new OpenApiComponentsPartialParsingStrategy(_unresolvedReferences);
                element.Components      = new OpenApiComponents();
                componentsNode.Parse(element.Components);
            }
        }
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiOAuthFlow;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "authorizationUrl":
                    element.AuthorizationUrl = childNode.GetSimpleValue <string>();
                    break;

                case "tokenUrl":
                    element.TokenUrl = childNode.GetSimpleValue <string>();
                    break;

                case "refreshUrl":
                    element.RefreshUrl = childNode.GetSimpleValue <string>();
                    break;

                case "scopes":
                    element.Scopes = (childNode.Value as ObjectParsingNode).CreateObject(n => (string)(n as ValueParsingNode).Value);
                    break;
                }
            }
        }
Beispiel #7
0
        private static void ProcessNodeStart(ByteArrayReadStream stream, Stack <ParsingNode> nodeStack)
        {
            if (!nodeStack.TryPeek(out var currentNode))
            {
                throw new MalformedWorldException();
            }

            if (currentNode.Children.Count == 0)
            {
                currentNode.DataEnd = stream.Position;
            }

            var childType = stream.ReadByte();

            if (stream.IsOver)
            {
                throw new MalformedWorldException();
            }

            var child = new ParsingNode {
                Type      = (NodeType)childType,
                DataBegin = stream.Position//  + sizeof(MarkupByte)
            };

            currentNode.Children.Add(child);
            nodeStack.Push(child);
        }
Beispiel #8
0
        public static void ParseTileAreaNode(ParsingTree parsingTree, ParsingNode tileAreaNode)
        {
            if (parsingTree == null)
            {
                throw new ArgumentNullException(nameof(parsingTree));
            }
            if (tileAreaNode == null)
            {
                throw new ArgumentNullException(nameof(tileAreaNode));
            }

            var stream = new ParsingStream(
                tree: parsingTree,
                node: tileAreaNode);

            var areaStartingX = stream.ReadUInt16();
            var areaStartingY = stream.ReadUInt16();
            var areaZ         = stream.ReadByte();

            foreach (var tileNode in tileAreaNode.Children)
            {
                ParseTileNode(
                    parsingTree: parsingTree,
                    tileNode: tileNode,
                    areaStartingX: areaStartingX,
                    areaStartingY: areaStartingY,
                    areaZ: areaZ);
            }
        }
Beispiel #9
0
 internal ASTNode(ParsingNode parsingNode)
 {
     ParsingNodes     = new ParsingNode[] { parsingNode };
     StartParsingNode = default;
     EndParsingNode   = default;
     NodeID           = _NodeID++;
 }
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiResponse;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "description":
                    element.Description = childNode.GetSimpleValue <string>();
                    break;

                case "headers":
                    element.Headers = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiHeader, OpenApiHeaderParsingStrategy>();
                    break;

                case "content":
                    element.Content = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiMediaType, OpenApiMediaTypeParsingStrategy>();
                    break;

                case "links":
                    element.Links = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiLink, OpenApiLinkParsingStrategy>();
                    break;
                }
            }
        }
Beispiel #11
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiExample;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "summary":
                    element.Summary = childNode.GetSimpleValue <string>();
                    break;

                case "description":
                    element.Description = childNode.GetSimpleValue <string>();
                    break;

                case "externalValue":
                    element.ExternalValue = childNode.GetSimpleValue <string>();
                    break;
                }
            }
        }
Beispiel #12
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiXml;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "name":
                    element.Name = childNode.GetSimpleValue <string>();
                    break;

                case "namespace":
                    element.Namespace = childNode.GetSimpleValue <string>();
                    break;

                case "prefix":
                    element.Prefix = childNode.GetSimpleValue <string>();
                    break;

                case "attribute":
                    element.Attribute = childNode.GetSimpleValue <bool>();
                    break;

                case "wrapped":
                    element.Wrapped = childNode.GetSimpleValue <bool>();
                    break;
                }
            }
        }
Beispiel #13
0
        private static void ParseTileNode(
            ParsingTree parsingTree,
            ParsingNode tileNode,
            UInt16 areaStartingX,
            UInt16 areaStartingY,
            Byte areaZ
            )
        {
            if (tileNode.Type != NodeType.NormalTile && tileNode.Type != NodeType.HouseTile)
            {
                throw new MalformedTileAreaNodeException("Unknow tile area node type.");
            }

            var stream = new ParsingStream(parsingTree, tileNode);

            var tileX = areaStartingX + stream.ReadByte();
            var tileY = areaStartingY + stream.ReadByte();

            Tile tile = null;

            if (tileNode.Type == NodeType.HouseTile)
            {
                tile = ParseHouseTile(ref stream, (ushort)tileX, (ushort)tileY, areaZ);                 // Improve this, remove casts
            }
            var tileFlags = ParseTileAttributes(ref parsingTree, ref stream, ref tile, tileNode);

            if (tile != null)
            {
                tile.Flags.AddFlags(tileFlags);
            }
        }
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiContact;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "name":
                    element.Name = childNode.GetSimpleValue <string>();
                    break;

                case "url":
                    element.Url = childNode.GetSimpleValue <string>();
                    break;

                case "email":
                    element.Email = childNode.GetSimpleValue <string>();
                    break;
                }
            }
        }
Beispiel #15
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiRequestBody;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "description":
                    element.Description = childNode.GetSimpleValue <string>();
                    break;

                case "required":
                    element.Required = childNode.GetSimpleValue <bool>();
                    break;

                case "content":
                    element.Content = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiMediaType, OpenApiMediaTypeParsingStrategy>();
                    break;
                }
            }
        }
Beispiel #16
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiServer;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "url":
                    element.Url = childNode.GetSimpleValue <string>();
                    break;

                case "description":
                    element.Description = childNode.GetSimpleValue <string>();
                    break;

                case "variables":
                    element.Variables = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiServerVariable, OpenApiServerVariableParsingStrategy>();
                    break;
                }
            }
        }
Beispiel #17
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiOAuthFlows;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "implicit":
                    element.Implicit = childNode.Value.ParseIntoElement <OpenApiOAuthFlow, OpenApiOAuthFlowParsingStrategy>();
                    break;

                case "password":
                    element.Password = childNode.Value.ParseIntoElement <OpenApiOAuthFlow, OpenApiOAuthFlowParsingStrategy>();
                    break;

                case "clientCredentials":
                    element.ClientCredentials = childNode.Value.ParseIntoElement <OpenApiOAuthFlow, OpenApiOAuthFlowParsingStrategy>();
                    break;

                case "authorizationCode":
                    element.AuthorizationCode = childNode.Value.ParseIntoElement <OpenApiOAuthFlow, OpenApiOAuthFlowParsingStrategy>();
                    break;
                }
            }
        }
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiMediaType;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "schema":
                    element.Schema = childNode.Value.ParseIntoElement <OpenApiSchema, OpenApiSchemaParsingStrategy>();
                    break;

                case "example":
                    // TODO
                    break;

                case "examples":
                    element.Examples = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiExample, OpenApiExampleParsingStrategy>();
                    break;
                }
            }
        }
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiComponents;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "schemas":
                    element.Schemas = (childNode.Value as ObjectParsingNode)
                                      .CreateObject <OpenApiSchema, OpenApiSchemaParsingStrategy>();
                    break;

                case "responses":
                    element.Responses = (childNode.Value as ObjectParsingNode)
                                        .CreateObject <OpenApiResponse, OpenApiResponseParsingStrategy>();
                    break;

                case "parameters":
                    element.Parameters = (childNode.Value as ObjectParsingNode)
                                         .CreateObject <OpenApiParameter, OpenApiParameterParsingStrategy>();
                    break;

                case "examples":
                    element.Examples = (childNode.Value as ObjectParsingNode)
                                       .CreateObject <OpenApiExample, OpenApiExampleParsingStrategy>();
                    break;

                case "requestBodies":
                    element.RequestBodies = (childNode.Value as ObjectParsingNode)
                                            .CreateObject <OpenApiRequestBody, OpenApiRequestBodyParsingStrategy>();
                    break;

                case "headers":
                    element.Headers = (childNode.Value as ObjectParsingNode)
                                      .CreateObject <OpenApiHeader, OpenApiHeaderParsingStrategy>();
                    break;

                case "securitySchemes":
                    element.SecuritySchemes = (childNode.Value as ObjectParsingNode)
                                              .CreateObject <OpenApiSecurityScheme, OpenApiSecuritySchemeParsingStrategy>();
                    break;

                case "links":
                    element.Links = (childNode.Value as ObjectParsingNode)
                                    .CreateObject <OpenApiLink, OpenApiLinkParsingStrategy>();
                    break;

                case "callbacks":
                    element.Callbacks = (childNode.Value as ObjectParsingNode)
                                        .CreateObject <OpenApiCallback, OpenApiCallbackParsingStrategy>();
                    break;
                }
            }
        }
Beispiel #20
0
            static ParsingNode MakeNode(Queue <ParsingNode> queue, ParsingNodeType nodeType)
            {
                var allTokens = queue.SelectMany(node => node.Tokens);
                var node      = new ParsingNode(nodeType, allTokens);

                queue.Clear();
                return(node);
            }
Beispiel #21
0
 private void ClearStorage(ParsingNode node)
 {
     node.storage.Remove("operation.consumes");
     node.storage.Remove("operation.produces");
     node.storage.Remove("operation.parameters");
     node.storage.Remove("operation.schemes");
     node.storage.Remove("operation.responses");
 }
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiHeader;

            foreach (var childNode in node.childNodes)
            {
                switch (childNode.Name)
                {
                case "description":
                    element.Description = childNode.GetSimpleValue <string>();
                    break;

                case "required":
                    element.Required = childNode.GetSimpleValue <bool>();
                    break;

                case "deprecated":
                    element.Deprecated = childNode.GetSimpleValue <bool>();
                    break;

                case "allowEmptyValue":
                    element.AllowEmptyValue = childNode.GetSimpleValue <bool>();
                    break;

                case "style":
                    element.Style = (OpenApiParameterStyle)Enum.Parse(typeof(OpenApiParameterStyle), childNode.GetSimpleValue <string>());
                    break;

                case "explode":
                    element.Explode = childNode.GetSimpleValue <bool>();
                    break;

                case "allowReserved":
                    element.AllowReserved = childNode.GetSimpleValue <bool>();
                    break;

                case "schema":
                    element.Schema = childNode.Value.ParseIntoElement <OpenApiSchema, OpenApiSchemaParsingStrategy>();
                    break;

                case "example":
                    // TODO
                    break;

                case "examples":
                    element.Examples = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiExample, OpenApiExampleParsingStrategy>();
                    break;

                case "content":
                    element.Content = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiMediaType, OpenApiMediaTypeParsingStrategy>();
                    break;
                }
            }
        }
Beispiel #23
0
        public OpenApiDocument ParsePartial(JObject json, string pathName, string operationName)
        {
            var rootNode = ParsingNode.Create(json, this);
            var document = new OpenApiDocument();

            rootNode.strategy = new OpenApiDocumentPartialParsingStrategy(pathName, operationName);
            rootNode.Parse(document);
            return(document);
        }
Beispiel #24
0
        public OpenApiDocument Parse(JObject json)
        {
            var rootNode = ParsingNode.Create(json, this);
            var document = new OpenApiDocument();

            rootNode.strategy = new OpenApiDocumentParsingStrategy();
            rootNode.Parse(document);
            return(document);
        }
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ValueParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiRuntimeExpressionOrAny;
            var expr    = OpenApiRuntimeExpression.Build((string)node.Value);

            element.Expression = expr;
        }
Beispiel #26
0
        public static OpenApiComponents CreateComponents(ParsingNode node)
        {
            var components  = new OpenApiComponents();
            var definitions = node.storage.Retrieve <IDictionary <string, OpenApiSchema> >("root.definitions");

            if (definitions != null)
            {
                components.Schemas = new Dictionary <string, OpenApiSchema>();
                foreach (var schema in definitions)
                {
                    components.Schemas.Add(schema);
                }
            }
            var parameters = node.storage.Retrieve <IDictionary <string, OpenApiParameter> >("root.parameters");

            if (parameters != null)
            {
                components.Parameters    = new Dictionary <string, OpenApiParameter>();
                components.RequestBodies = new Dictionary <string, OpenApiRequestBody>();
                foreach (var parameter in parameters)
                {
                    if (parameter.Value.In == OpenApiParameterLocation.Body || parameter.Value.In == OpenApiParameterLocation.FormData)
                    {
                        components.RequestBodies.Add(parameter.Key, BodyParameterToRequestBody(node, parameter.Value));
                    }
                    else
                    {
                        components.Parameters.Add(parameter);
                    }
                }
            }
            var responses = node.storage.Retrieve <IDictionary <string, OpenApiResponse> >("root.responses");

            if (responses != null)
            {
                components.Responses = new OpenApiResponses();
                foreach (var response in responses)
                {
                    components.Responses.Add(response);
                }
            }
            var securityDefinitions = node.storage.Retrieve <IDictionary <string, OpenApiSecurityScheme> >("root.securityDefinitions");

            if (securityDefinitions != null)
            {
                components.SecuritySchemes = new Dictionary <string, OpenApiSecurityScheme>();
                foreach (var scheme in securityDefinitions)
                {
                    components.SecuritySchemes.Add(scheme);
                }
            }
            return(components);
        }
 private void ClearStorage(ParsingNode node)
 {
     node.storage.Remove("root.host");
     node.storage.Remove("root.basePath");
     node.storage.Remove("root.schemes");
     node.storage.Remove("root.produces");
     node.storage.Remove("root.consumes");
     node.storage.Remove("root.definitions");
     node.storage.Remove("root.parameters");
     node.storage.Remove("root.responses");
     node.storage.Remove("root.securityDefinitions");
 }
Beispiel #28
0
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiResponses;

            foreach (var childNode in node.childNodes)
            {
                element.Add(childNode.Name, childNode.Value.ParseIntoElement <OpenApiResponse, OpenApiResponseParsingStrategy>());
            }
        }
        public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement)
        {
            if (!(parsingNode is ObjectParsingNode node))
            {
                throw new ArgumentException();
            }
            var element = parsingElement as OpenApiCallback;

            foreach (var childNode in node.childNodes)
            {
                var pathItemExpr = childNode.Name;
                var pathItem     = childNode.Value.ParseIntoElement <OpenApiPathItem, OpenApiPathItemParsingStrategy>();
                element.PathItems.Add(OpenApiRuntimeExpression.Build(pathItemExpr), pathItem);
            }
        }
Beispiel #30
0
 internal ExpressionNode(JinjaEnvironment environment, ParsingNode parsingNode,
                         WhiteSpaceMode startWhiteSpace, WhiteSpaceMode endWhiteSpace, string expression, string?ifClause, string?elseClause) : base(parsingNode)
 {
     Expression           = expression;
     ExpressionParserNode = environment.Evaluation.Parse(expression);
     if (ifClause != null)
     {
         IfClause = environment.Evaluation.Parse(ifClause);
     }
     if (elseClause != null)
     {
         ElseClause = new ExpressionNode(environment, elseClause);
     }
     WhiteSpaceControl = new WhiteSpaceControlSet(startWhiteSpace, endWhiteSpace);
 }