/// <summary> /// Resolves a JSON Pointer with respect to an element, returning the referenced element. /// </summary> /// <param name="element">The referencable Open API element on which to apply the JSON pointer</param> /// <param name="pointer">a JSON Pointer [RFC 6901](https://tools.ietf.org/html/rfc6901).</param> /// <returns>The element pointed to by the JSON pointer.</returns> public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable element, JsonPointer pointer) { if (!pointer.Tokens.Any()) { return(element); } var propertyName = pointer.Tokens.FirstOrDefault(); var mapKey = pointer.Tokens.ElementAtOrDefault(1); try { if (element.GetType() == typeof(OpenApiHeader)) { return(ResolveReferenceOnHeaderElement((OpenApiHeader)element, propertyName, mapKey, pointer)); } if (element.GetType() == typeof(OpenApiParameter)) { return(ResolveReferenceOnParameterElement((OpenApiParameter)element, propertyName, mapKey, pointer)); } if (element.GetType() == typeof(OpenApiResponse)) { return(ResolveReferenceOnResponseElement((OpenApiResponse)element, propertyName, mapKey, pointer)); } } catch (KeyNotFoundException) { throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer)); } throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer)); }
public void ResolveReferenceShouldThrowOnInvalidReferenceId(IOpenApiReferenceable element, string jsonPointer) { // Act Action resolveReference = () => element.ResolveReference(new JsonPointer(jsonPointer)); // Assert var exception = Assert.Throws <OpenApiException>(resolveReference); Assert.Equal(string.Format(SRResource.InvalidReferenceId, jsonPointer), exception.Message); }
/// <summary> /// Identify if an element is just a reference to a component, or an actual component /// </summary> private bool ProcessAsReference(IOpenApiReferenceable referenceable, bool isComponent = false) { var isReference = referenceable.Reference != null && !isComponent; if (isReference) { Walk(referenceable); } return(isReference); }
public void ResolveReferenceCanResolveValidJsonPointers( IOpenApiReferenceable element, string jsonPointer, IOpenApiElement expectedResolvedElement) { // Act var actualResolvedElement = element.ResolveReference(new JsonPointer(jsonPointer)); // Assert Assert.Same(expectedResolvedElement, actualResolvedElement); }
public override void Visit(IOpenApiReferenceable referenceable) { switch (referenceable) { case OpenApiSchema schema: if (!Schemas.ContainsKey(schema.Reference.Id)) { Schemas.Add(schema.Reference.Id, schema); } break; default: break; } base.Visit(referenceable); }
public override void Visit(IOpenApiReferenceable referenceable) { switch (referenceable) { case OpenApiSchema schema: EnsureComponentsExists(); EnsureSchemasExists(); if (!Components.Schemas.ContainsKey(schema.Reference.Id)) { Components.Schemas.Add(schema.Reference.Id, schema); } break; case OpenApiParameter parameter: EnsureComponentsExists(); EnsureParametersExists(); if (!Components.Parameters.ContainsKey(parameter.Reference.Id)) { Components.Parameters.Add(parameter.Reference.Id, parameter); } break; case OpenApiResponse response: EnsureComponentsExists(); EnsureResponsesExists(); if (!Components.Responses.ContainsKey(response.Reference.Id)) { Components.Responses.Add(response.Reference.Id, response); } break; default: break; } base.Visit(referenceable); }
/// <summary> /// Visits <see cref="OpenApiSecurityScheme"/> and child objects /// </summary> internal void Walk(IOpenApiReferenceable referenceable) { _visitor.Visit(referenceable); }
private bool IsUnresolvedReference(IOpenApiReferenceable possibleReference) { return(possibleReference != null && possibleReference.UnresolvedReference); }
/// <summary> /// Visits IOpenApiReferenceable instances that are references and not in components /// </summary> /// <param name="referenceable">referenced object</param> public virtual void Visit(IOpenApiReferenceable referenceable) { }
public override void Visit(IOpenApiReferenceable referenceable) { Locations.Add("referenceAt: " + this.PathString); }
/// <summary> /// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object /// </summary> public bool TryLoadReference(OpenApiReference reference, out IOpenApiReferenceable referencedObject) { referencedObject = null; if (reference == null) { return(false); } if (reference.IsExternal) { // TODO: need to read the external document and load the referenced object. throw new NotImplementedException(SRResource.LoadReferencedObjectFromExternalNotImplmented); } if (!reference.Type.HasValue) { throw new ArgumentException("Local reference must have type specified."); } // Special case for Tag if (reference.Type == ReferenceType.Tag) { foreach (var tag in _tags) { if (tag.Name == reference.Id) { referencedObject = tag; return(true); } } referencedObject = new OpenApiTag { Name = reference.Id }; return(false); } var componentJsonPointer = new JsonPointer("#/components/" + reference.Type.GetDisplayName() + "/" + reference.Id); var node = _rootNode.Find(componentJsonPointer); switch (reference.Type) { case ReferenceType.Schema: referencedObject = OpenApiV3Deserializer.LoadSchema(node); break; case ReferenceType.Response: referencedObject = OpenApiV3Deserializer.LoadResponse(node); break; case ReferenceType.Parameter: referencedObject = OpenApiV3Deserializer.LoadParameter(node); break; case ReferenceType.Example: referencedObject = OpenApiV3Deserializer.LoadExample(node); break; case ReferenceType.RequestBody: referencedObject = OpenApiV3Deserializer.LoadRequestBody(node); break; case ReferenceType.Header: referencedObject = OpenApiV3Deserializer.LoadHeader(node); break; case ReferenceType.SecurityScheme: referencedObject = OpenApiV3Deserializer.LoadSecurityScheme(node); break; case ReferenceType.Link: referencedObject = OpenApiV3Deserializer.LoadLink(node); break; case ReferenceType.Callback: referencedObject = OpenApiV3Deserializer.LoadCallback(node); break; default: throw new OpenApiException( string.Format( SRResource.ReferenceV3HasInvalidValue, reference.Type, componentJsonPointer)); } return(true); }
/// <summary> /// Collect reference for each reference /// </summary> /// <param name="referenceable"></param> public override void Visit(IOpenApiReferenceable referenceable) { AddReference(referenceable.Reference); }
public override void Visit(IOpenApiReferenceable referenceable) { EncodeCall(); base.Visit(referenceable); }
/// <summary> /// Identify if an element is just a reference to a component, or an actual component /// </summary> private bool IsReference(IOpenApiReferenceable referenceable) { return(referenceable.Reference != null && !_inComponents); }
/// <summary> /// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object /// </summary> public bool TryLoadReference(ParsingContext context, OpenApiReference reference, out IOpenApiReferenceable referencedObject) { referencedObject = null; if (reference == null) { return(false); } if (reference.IsExternal) { // TODO: need to read the external document and load the referenced object. throw new NotImplementedException(SRResource.LoadReferencedObjectFromExternalNotImplmented); } if (!reference.Type.HasValue) { throw new ArgumentException("Local reference must have type specified."); } // Special case for Tag if (reference.Type == ReferenceType.Tag) { foreach (var tag in context.Tags) { if (tag.Name == reference.Id) { referencedObject = tag; return(true); } } referencedObject = new OpenApiTag { Name = reference.Id }; return(false); } var jsonPointer = new JsonPointer("#/" + GetReferenceTypeV2Name(reference.Type.Value) + "/" + reference.Id); var node = context.RootNode.Find(jsonPointer); switch (reference.Type) { case ReferenceType.Schema: referencedObject = OpenApiV2Deserializer.LoadSchema(node); break; case ReferenceType.Response: referencedObject = OpenApiV2Deserializer.LoadResponse(node); break; case ReferenceType.Parameter: // TODO: Handle referencing to a "body" parameter in V2 referencedObject = OpenApiV2Deserializer.LoadParameter(node); break; case ReferenceType.SecurityScheme: referencedObject = OpenApiV2Deserializer.LoadSecurityScheme(node); break; default: throw new OpenApiException( string.Format( SRResource.ReferenceV2HasInvalidValue, reference.Type, jsonPointer)); } return(true); }
/// <summary> /// Adds a fragment of an OpenApiDocument to the workspace. /// </summary> /// <param name="location"></param> /// <param name="fragment"></param> /// <remarks>Not sure how this is going to work. Does the reference just point to the fragment as a whole, or do we need to /// to be able to point into the fragment. Keeping it private until we figure it out. /// </remarks> public void AddFragment(string location, IOpenApiReferenceable fragment) { _fragments.Add(ToLocationUrl(location), fragment); }