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);
            }
        }
Beispiel #2
0
        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()
            });
        }