private async Task <JToken> GetRefJTokenAsync(RefInfo refInfo) { var loader = GetReferenceLoader(refInfo); var replacement = await loader.GetDocumentPartAsync(refInfo.InDocumentPath); return(replacement); }
private ReferenceLoader GetReferenceLoader(RefInfo refInfo) { if (refInfo.IsNestedInThisDocument) { return(this); } if (_loaders.TryGetValue(refInfo.AbsoluteDocumentUri, out var loader)) { return(loader); } loader = new ReferenceLoader(refInfo.AbsoluteDocumentUri, _loaders, _settings); return(loader); }
private async Task ResolveRefAsync(JToken token) { if (!(token is JContainer container)) { return; } var refProps = container.Descendants() .OfType <JProperty>() .Where(p => p.Name == Constants.REF_KEYWORD) .ToList(); foreach (var refProperty in refProps) { var refPath = refProperty.Value.ToString(); var refInfo = RefInfo.GetRefInfo(_documentUri, refPath); if (!_settings.ShouldResolveReference(refInfo)) { continue; } var replacement = await GetRefJTokenAsync(refInfo); await ResolveRefAsync(replacement); if (refProperty.Parent?.Parent == null) { // When a property has already been replaced by recursions, it has no more Parent continue; } // clone to avoid destroying the original documents replacement = replacement.DeepClone(); if (replacement is JObject jobject) { jobject[Constants.FROM_REF_PROPERTY_NAME] = refPath; jobject[Constants.REF_NAME_PROPERTY_NAME] = refInfo.RefFriendlyName; } _settings.ApplyRefReplacement(refInfo, _rootJObj, refProperty, replacement, refInfo.AbsoluteDocumentUri); } }
OpenApiRefResolver GetYamlLoader(RefInfo refInfo) { if (refInfo.IsNestedInThisDocument) { return(this); } if (refInfo.AbsoluteDocumentUri == DocumentUri) { return(this); } if (OtherResolvers.TryGetValue(refInfo.AbsoluteDocumentUri, out var loader)) { return(loader); } loader = new OpenApiRefResolver(refInfo.AbsoluteDocumentUri, Authorization, OtherResolvers); OtherResolvers[refInfo.AbsoluteDocumentUri] = loader; return(loader); }
public async Task <RefPropertyHolder> GetRefJTokenAsync(string refPath) { var refInfo = RefInfo.GetRefInfo(DocumentUri, refPath); var loader = GetYamlLoader(refInfo); var propertyValue = await loader.GetDocumentPartAsync(refInfo.InDocumentPath, true); var refSplit = refPath.Split('/'); var propertyType = refSplit[refSplit.Length - 2]; //example: definitions var propertyName = refSplit.Last(); Dictionary <string, RefPropertyHolder> subProperties = propertyValue is JContainer container ? GetSubProperties(loader, container) : null; return(new RefPropertyHolder { PropertyName = propertyName, PropertyType = propertyType, PropertyValue = propertyValue, SubProperties = subProperties?.Values.ToArray() }); }