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; } } }
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 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; } } }
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; } } }
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; } } }
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; } } }
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; } } }
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; } } }
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 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; } } }
/// <summary> /// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element. /// </summary> /// <param name="input">TextReader containing OpenAPI description to parse.</param> /// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param> /// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing</param> /// <returns>Instance of newly created OpenApiDocument</returns> public T ReadFragment <T>(YamlDocument input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic) where T : IOpenApiElement { diagnostic = new OpenApiDiagnostic(); var context = new ParsingContext(diagnostic) { ExtensionParsers = _settings.ExtensionParsers }; IOpenApiElement element = null; try { // Parse the OpenAPI element element = context.ParseFragment <T>(input, version); } catch (OpenApiException ex) { diagnostic.Errors.Add(new OpenApiError(ex)); } // Validate the element if (_settings.RuleSet != null && _settings.RuleSet.Rules.Count > 0) { var errors = element.Validate(_settings.RuleSet); foreach (var item in errors) { diagnostic.Errors.Add(item); } } return((T)element); }
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; } } }
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; } } }
/// <summary> /// Validate element and all child elements /// </summary> /// <param name="element">Element to validate</param> /// <param name="ruleSet">Optional set of rules to use for validation</param> /// <returns>An IEnumerable of errors. This function will never return null.</returns> public static IEnumerable <ValidationError> Validate(this IOpenApiElement element, ValidationRuleSet ruleSet = null) { var validator = new OpenApiValidator(ruleSet); var walker = new OpenApiWalker(validator); walker.Walk(element); return(validator.Errors); }
/// <summary> /// Parse the contents of this node into an OpenApiElement /// </summary> /// <param name="openApiElement"> Element to parse content into. </param> public virtual void Parse(IOpenApiElement openApiElement) { if (strategy == null) { throw new Exception("Strategy to parse node unspecified."); } strategy.Parse(this, openApiElement); }
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; } } }
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; }
public void ResolveReferenceCanResolveValidJsonPointers( IOpenApiReferenceable element, string jsonPointer, IOpenApiElement expectedResolvedElement) { // Act var actualResolvedElement = element.ResolveReference(new JsonPointer(jsonPointer)); // Assert Assert.Same(expectedResolvedElement, actualResolvedElement); }
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>()); } }
/// <summary> /// If element to parse into has a reference link, find the reference before parsing it. /// </summary> /// <param name="openApiElement"> Element to parse into. </param> public override void Parse(IOpenApiElement openApiElement) { if (openApiElement is IOpenApiReferenceable referenceable) { var refLink = GetRefLink(); if (refLink != null) { referenceable.UnresolvedReference = true; referenceable.Reference = this.versionParser.CreateReference(referenceable.ReferenceType, refLink); } } base.Parse(openApiElement); }
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); } }
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 "schema": element.Content = new Dictionary <string, OpenApiMediaType>(); var produces = node.storage.Retrieve <IList <string> >("operation.produces"); if (produces == null) { produces = node.storage.Retrieve <IList <string> >("root.produces"); } if (produces == null) { produces = new List <string> { "application/json" }; } var schema = childNode.Value.ParseIntoElement <OpenApiSchema, OpenApiSchemaParsingStrategy>(); foreach (var mediaType in produces) { element.Content.Add(mediaType, new OpenApiMediaType { Schema = schema }); } break; case "headers": element.Headers = (childNode.Value as ObjectParsingNode).CreateObject <OpenApiHeader, OpenApiHeaderParsingStrategy>(); break; case "examples": break; } } }
public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement) { if (!(parsingNode is ObjectParsingNode node)) { throw new ArgumentException(); } var element = parsingElement as OpenApiPaths; foreach (var childNode in node.childNodes) { if (childNode.Name.StartsWith("/")) { element.Add(childNode.Name, childNode.Value.ParseIntoElement <OpenApiPathItem, OpenApiPathItemParsingStrategy>()); } } }
public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement) { if (!(parsingNode is ObjectParsingNode node)) { throw new ArgumentException(); } var element = parsingElement as OpenApiPaths; var pathNode = node.childNodes.Where(c => c.Name == _pathName).First().Value; // find correct path in JSON pathNode.strategy = new OpenApiPathItemPartialParsingStrategy(_operationName); var pathItem = new OpenApiPathItem(); pathNode.Parse(pathItem); // parse JSON into new PathItem object element.Add(_pathName, pathItem); }
/// <summary> /// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element. /// </summary> /// <param name="input">Stream containing OpenAPI description to parse.</param> /// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param> /// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing</param> /// <returns>Instance of newly created OpenApiDocument</returns> public T ReadFragment <T>(Stream input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic) where T : IOpenApiElement { ParsingContext context; YamlDocument yamlDocument; diagnostic = new OpenApiDiagnostic(); // Parse the YAML/JSON try { yamlDocument = LoadYamlDocument(input); } catch (YamlException ex) { diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message)); return(default(T)); } context = new ParsingContext { ExtensionParsers = _settings.ExtensionParsers }; IOpenApiElement element = null; try { // Parse the OpenAPI element element = context.ParseFragment <T>(yamlDocument, version, diagnostic); } catch (OpenApiException ex) { diagnostic.Errors.Add(new OpenApiError(ex)); } // Validate the element if (_settings.RuleSet != null && _settings.RuleSet.Rules.Count > 0) { var errors = element.Validate(_settings.RuleSet); foreach (var item in errors) { diagnostic.Errors.Add(item); } } return((T)element); }
public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement) { if (!(parsingNode is ObjectParsingNode node)) { throw new ArgumentException(); } var element = parsingElement as OpenApiSecurityScheme; foreach (var childNode in node.childNodes) { switch (childNode.Name) { case "type": element.Type = (OpenApiSecuritySchemeType)Enum.Parse(typeof(OpenApiSecuritySchemeType), childNode.GetSimpleValue <string>(), true); break; case "description": element.Description = childNode.GetSimpleValue <string>(); break; case "name": element.Name = childNode.GetSimpleValue <string>(); break; case "in": element.In = (OpenApiParameterLocation)Enum.Parse(typeof(OpenApiParameterLocation), childNode.GetSimpleValue <string>(), true); break; case "scheme": element.Scheme = childNode.GetSimpleValue <string>(); break; case "bearerFormat": element.BearerFormat = childNode.GetSimpleValue <string>(); break; case "flows": element.Flows = childNode.Value.ParseIntoElement <OpenApiOAuthFlows, OpenApiOAuthFlowsParsingStrategy>(); break; case "openIdConnectUrl": element.OpenIdConnectUrl = childNode.GetSimpleValue <string>(); break; } } }
public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement) { if (!(parsingNode is ObjectParsingNode node)) { throw new ArgumentException(); } var element = parsingElement as OpenApiComponents; foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.Schema)) { AddSchema(node.childNodes, element, reference); } foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.Response)) { AddResponse(node.childNodes, element, reference); } foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.Parameter)) { AddParameter(node.childNodes, element, reference); } foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.Example)) { AddExample(node.childNodes, element, reference); } foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.RequestBody)) { AddRequestBody(node.childNodes, element, reference); } foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.Header)) { AddHeader(node.childNodes, element, reference); } foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.SecurityScheme)) { AddSecurityScheme(node.childNodes, element, reference); } foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.Link)) { AddLink(node.childNodes, element, reference); } foreach (var reference in _unresolvedReferences.Where(r => r.Type == OpenApiReferenceType.Callback)) { AddCallback(node.childNodes, element, reference); } }
public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement) { if (!(parsingNode is ObjectParsingNode node)) { throw new ArgumentException(); } var element = parsingElement as OpenApiSecurityRequirement; foreach (var childNode in node.childNodes) { var requirements = new List <string>(); foreach (var name in (childNode.Value as ListParsingNode).childNodes) { requirements.Add((string)(name as ValueParsingNode).Value); } element.Add(childNode.Name, requirements); } }
public void Parse(ParsingNode parsingNode, IOpenApiElement parsingElement) { if (!(parsingNode is ObjectParsingNode node)) { throw new ArgumentException(); } var element = parsingElement as OpenApiPaths; foreach (var childNode in node.childNodes) { if (childNode.Name.StartsWith("/")) // a path { var pathItem = new OpenApiPathItem(); childNode.Value.strategy = new OpenApiPathItemParsingStrategy(); childNode.Value.Parse(pathItem); element.Add(childNode.Name, pathItem); } } }