Exemple #1
0
 /// <summary>
 /// Register all the IJsonSchemaDefinition generated in one go to avoid ordering problems for references
 /// </summary>
 /// <param name="jsonSchema"></param>
 public void Register(IJsonSchemaDefinition jsonSchema)
 {
     RegisteredJsonSchemas.Add(jsonSchema.FileName, jsonSchema);
 }
Exemple #2
0
        /// <summary>
        /// Returns null if not found
        /// </summary>
        /// <param name="relativeFileNameWithExpandedDot"></param>
        /// <param name="jSBRef"></param>
        /// <returns></returns>
        public (IJSBPart refPart, JsonValue schemaValue) LookupReferencedPart(string relativeFileNameWithExpandedDot, JSBRef jSBRef)
        {
            if (RegisteredJsonSchemas.TryGetValue(relativeFileNameWithExpandedDot, out IJsonSchemaDefinition jsonSchemaDefinition))
            {
                if (jSBRef.Fragment.ToLowerInvariant().StartsWith("/definitions/"))
                {
                    string definitionKey = TransformToTitleCase(jSBRef.Fragment.Substring("/definitions/".Length));
                    if (jsonSchemaDefinition.JsonSchemaBuilderSchema.Definitions.TryGetValue(definitionKey, out IJSBPart referencedPart))
                    {
                        return(referencedPart, null);
                    }
                }
                else
                {
                    return(jsonSchemaDefinition.JsonSchemaBuilderSchema, null);
                }
            }

            //Search for the schema file as .schema.json and .json and load it when found
            string schemaString = string.Empty;

            string localfile = Path.Combine(JsonSchemaApplicationRoot, relativeFileNameWithExpandedDot);

            if (File.Exists(localfile))
            {
                schemaString = File.ReadAllText(localfile);
            }
            else
            {
                throw new CodeGenerationException($"Schema could not be found at the path {localfile}");
            }

            // make a schema and generate code from that
            JsonValue jsonValueOfSchema = JsonValue.Parse(schemaString);

            if (string.IsNullOrWhiteSpace(jSBRef.Fragment) ||
                jSBRef.Fragment.Equals("/"))
            {
                return(null, jsonValueOfSchema);
            }
            else if (jSBRef.Fragment.ToLowerInvariant().StartsWith("/definitions/"))
            {
                //TODO Avoid serialization step
                JsonSerializer jsonSerializer   = new JsonSerializer();
                JsonSchema     jsonSchema       = jsonSerializer.Deserialize <JsonSchema>(jsonValueOfSchema);
                string         afterDefinitions = jSBRef.Fragment.ToLowerInvariant().Replace("/definitions/", "");
                if (jsonSchema.Definitions() != null && jsonSchema.Definitions().TryGetValue(afterDefinitions, out JsonSchema subSchema))
                {
                    //Process subschema Json
                    return(null, subSchema.ToJson(jsonSerializer));
                }
                else
                {
                    throw new CodeGenerationException($"Could not find {afterDefinitions} in the definitions of the schema");
                }
            }
            else
            {
                throw new NotImplementedException($"Uri reference is not supported other than /definitions/ or entire schema");
            }
        }