public void AsJsonSchema_ConvertXsdToJsonSchema_CorrectNumberOfPropertiesAndDefinitions(string xsdPath) { // Arrange using XmlReader xsdReader = XmlReader.Create(TestDataHelper.LoadTestDataFromFile(xsdPath)); XsdToJsonSchema target = new XsdToJsonSchema(xsdReader); // Act JsonSchema actual = target.AsJsonSchema(); // Assert Assert.NotNull(actual); Assert.Single(actual.Properties()); Assert.Equal(6, actual.Definitions().Count); }
public void AsJsonSchema_ConvertXsdToJsonSchema_CorrectNumberOfPropertiesAndDefinitions() { // Arrange XmlReader xsdReader = XmlReader.Create(LoadTestData("Model/Xsd/melding-1603-12392.xsd")); XsdToJsonSchema target = new XsdToJsonSchema(xsdReader); // Act JsonSchema actual = target.AsJsonSchema(); // Assert Assert.NotNull(actual); Assert.Equal(12, actual.Properties().Count); Assert.Equal(19, actual.Definitions().Count); }
/// <summary> /// Initializes a new instance of the <see cref="JsonSchemaToInstanceModelGenerator"/> class. /// Creates an initial JSON Instance Model. Assumes top object has properties and that there are multiple definitions. /// <see cref="GetInstanceModel"> to get the model </see>"/> /// </summary> /// <param name="organizationName">The organisation name</param> /// <param name="serviceName">Service name</param> /// <param name="jsonSchema">The Json Schema to generate the instance model from</param> /// <param name="multiplicityString">String to append for marking arrays</param> public JsonSchemaToInstanceModelGenerator(string organizationName, string serviceName, JsonSchema jsonSchema, string multiplicityString = "[*]") { this.jsonSchema = jsonSchema; this.multiplicityString = multiplicityString; instanceModel.Add("Org", organizationName); instanceModel.Add("ServiceName", serviceName); instanceModel.Add("Elements", elements); foreach (KeyValuePair <string, JsonSchema> def in jsonSchema.Definitions()) { definitions.Add(def.Key, def.Value); } GenerateInitialReferences(); }
public void ConvertXsdToJsonSchemaAndBack_CorrectNumberOfPropertiesAndDefinitions() { // Arrange XmlReader xsdReader = XmlReader.Create(LoadTestData("Designer.Tests._TestData.xsd.melding-1603-12392.xsd")); XsdToJsonSchema target = new XsdToJsonSchema(xsdReader); // Act JsonSchema actual = target.AsJsonSchema(); JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd(); XmlSchema xmlschema = jsonSchemaToXsd.CreateXsd(actual); FileStream file = new FileStream("Designer.Tests._TestData.xsd.melding-1603-12392b.xsd", FileMode.Create, FileAccess.ReadWrite); XmlTextWriter xwriter = new XmlTextWriter(file, new UTF8Encoding()); xwriter.Formatting = Formatting.Indented; xmlschema.Write(xwriter); // Assert Assert.NotNull(actual); Assert.Equal(12, actual.Properties().Count); Assert.Equal(19, actual.Definitions().Count); }
/// <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"); } }