Esempio n. 1
0
        public void ResolveDeferedSchemas()
        {
            if (_deferedSchemas.Count > 0)
            {
                List <DeferedSchema> resolvedDeferedSchemas = new List <DeferedSchema>();

                // note that defered schemas could be added while resolving
                while (_deferedSchemas.Count > 0)
                {
                    DeferedSchema deferedSchema = _deferedSchemas[0];

                    ResolveDeferedSchema(deferedSchema);

                    resolvedDeferedSchemas.Add(deferedSchema);
                    _deferedSchemas.Remove(deferedSchema);
                }

                foreach (DeferedSchema resolvedDeferedSchema in resolvedDeferedSchemas)
                {
                    if (!resolvedDeferedSchema.Success)
                    {
                        throw new JsonException("Could not resolve schema reference '{0}'.".FormatWith(CultureInfo.InvariantCulture, resolvedDeferedSchema.ResolvedReference));
                    }
                }
            }
        }
Esempio n. 2
0
        private void AddDeferedSchema(Uri resolvedReference, JSchema referenceSchema, Action <JSchema> setSchema)
        {
            DeferedSchema deferedSchema = _deferedSchemas.SingleOrDefault(d => UriComparer.Instance.Equals(d.ResolvedReference, resolvedReference));

            if (deferedSchema == null)
            {
                deferedSchema = new DeferedSchema(resolvedReference, referenceSchema);
                _deferedSchemas.Add(deferedSchema);
            }

            deferedSchema.AddSchemaSet(setSchema);
        }
Esempio n. 3
0
        private void LoadAndSetSchema(JsonReader reader, Action <JSchema> setSchema, bool resolveDeferedImmediately = false)
        {
            JSchema schema = new JSchema();

            _schemaStack.Push(schema);

            ReadSchema(reader, schema);

            //schema.Reference;

            if (schema.Reference != null)
            {
                Uri resolvedReference = null;
                foreach (JSchema s in _schemaStack.Reverse())
                {
                    Uri part = s.Id;

                    if (part != null)
                    {
                        if (resolvedReference == null)
                        {
                            resolvedReference = part;
                        }
                        else
                        {
                            resolvedReference = SchemaDiscovery.ResolveSchemaId(resolvedReference, part);
                        }
                    }
                }

                resolvedReference = SchemaDiscovery.ResolveSchemaId(resolvedReference, schema.Reference);

                JSchema resolvedSchema = _resolver.GetSchema(resolvedReference);
                if (resolvedSchema != null)
                {
                    schema = resolvedSchema;
                }
                else
                {
                    DeferedSchema deferedSchema = new DeferedSchema(resolvedReference, schema, setSchema);
                    _deferedSchemas.Add(deferedSchema);
                }
            }

            setSchema(schema);

            _schemaStack.Pop();
        }
Esempio n. 4
0
        public void ResolveDeferedSchemas()
        {
            if (_deferedSchemas.Count > 0)
            {
                List <DeferedSchema> resolvedDeferedSchemas = new List <DeferedSchema>();

                int initialCount = _deferedSchemas.Count;

                int i = 0;
                // note that defered schemas could be added while resolving
                while (_deferedSchemas.Count > 0)
                {
                    DeferedSchema deferedSchema = _deferedSchemas[0];

                    ResolveDeferedSchema(deferedSchema);

                    resolvedDeferedSchemas.Add(deferedSchema);
                    _deferedSchemas.Remove(deferedSchema);

                    if (!deferedSchema.Success && i >= initialCount)
                    {
                        // if schema resolved to another reference and that reference has already not be resolved then fail
                        // probably a circular reference
                        for (int j = initialCount; j < resolvedDeferedSchemas.Count; j++)
                        {
                            DeferedSchema resolvedSchema = resolvedDeferedSchemas[j];
                            if (deferedSchema != resolvedSchema && deferedSchema.ResolvedReference.ToString() == resolvedSchema.ResolvedReference.ToString())
                            {
                                throw JSchemaReaderException.Create(resolvedSchema.ReferenceSchema, _baseUri, resolvedSchema.ReferenceSchema.Path, "Could not resolve schema reference '{0}'.".FormatWith(CultureInfo.InvariantCulture, deferedSchema.ResolvedReference));
                            }
                        }
                    }
                    i++;
                }

                foreach (DeferedSchema resolvedDeferedSchema in resolvedDeferedSchemas)
                {
                    if (!resolvedDeferedSchema.Success)
                    {
                        throw JSchemaReaderException.Create(resolvedDeferedSchema.ReferenceSchema, _baseUri, resolvedDeferedSchema.ReferenceSchema.Path, "Could not resolve schema reference '{0}'.".FormatWith(CultureInfo.InvariantCulture, resolvedDeferedSchema.ResolvedReference));
                    }
                }
            }
        }
Esempio n. 5
0
        private void ResolveDeferedSchema(DeferedSchema deferedSchema)
        {
            Uri reference = deferedSchema.ResolvedReference;

            bool found = SchemaDiscovery.FindSchema(s =>
            {
                // additional json copied to referenced schema
                // kind of hacky
                if (deferedSchema.ReferenceSchema._extensionData != null)
                {
                    foreach (KeyValuePair <string, JToken> keyValuePair in deferedSchema.ReferenceSchema._extensionData.ToList())
                    {
                        s.ExtensionData[keyValuePair.Key] = keyValuePair.Value;
                    }
                }

                deferedSchema.SetResolvedSchema(s);
            }, RootSchema, RootSchema.Id, reference, this, ref _schemaDiscovery);

            if (found)
            {
                return;
            }

            JSchema resolvedSchema;

            try
            {
                resolvedSchema = ResolvedSchema(deferedSchema.ReferenceSchema.Reference, deferedSchema.ResolvedReference);
            }
            catch (Exception ex)
            {
                throw JSchemaReaderException.Create(deferedSchema.ReferenceSchema, _baseUri, deferedSchema.ReferenceSchema.Path, "Error when resolving schema reference '{0}'.".FormatWith(CultureInfo.InvariantCulture, deferedSchema.ReferenceSchema.Reference), ex);
            }

            if (resolvedSchema != null)
            {
                deferedSchema.SetResolvedSchema(resolvedSchema);
                return;
            }

            throw JSchemaReaderException.Create(deferedSchema.ReferenceSchema, _baseUri, deferedSchema.ReferenceSchema.Path, "Could not resolve schema reference '{0}'.".FormatWith(CultureInfo.InvariantCulture, deferedSchema.ResolvedReference));
        }
Esempio n. 6
0
        private void ResolveDeferedSchema(DeferedSchema deferedSchema)
        {
            Uri reference = deferedSchema.ResolvedReference;

            bool found = SchemaDiscovery.FindSchema(s =>
            {
                // additional json copied to referenced schema
                // kind of hacky
                if (deferedSchema.ReferenceSchema._extensionData != null)
                {
                    foreach (KeyValuePair <string, JToken> keyValuePair in deferedSchema.ReferenceSchema._extensionData.ToList())
                    {
                        s.ExtensionData[keyValuePair.Key] = keyValuePair.Value;
                    }
                }

                deferedSchema.SetResolvedSchema(s);
            }, RootSchema, RootSchema.Id, reference, this);

            if (!found)
            {
                throw new JsonException("Could not resolve schema reference '{0}'.".FormatWith(CultureInfo.InvariantCulture, deferedSchema.ResolvedReference));
            }
        }
 public static DeferedSchemaKey CreateKey(DeferedSchema deferedSchema)
 {
     return(new DeferedSchemaKey(deferedSchema.ResolvedReference, deferedSchema.DynamicAnchor != null ? deferedSchema.ScopeId : null));
 }