public void BuildReference_Relative() { string refName = "refName"; string refDesc = "Description of refName"; Uri uri = new Uri("./address.schema.json", UriKind.Relative); var varref = new JSBRef(refName, refDesc, uri); JsonSchema varSchema = varref.AsJsonSchema(); }
private (IJSBPart refPart, JsonValue schemaValue) LookupReferencedPart(JSBRef jSBRef) { string relativeLocalFileWithExpandedDot = ExpandRelativelocalFile(jSBRef.RelativeLocalFile); if (string.IsNullOrWhiteSpace(relativeLocalFileWithExpandedDot)) { relativeLocalFileWithExpandedDot = Path.Combine(_startNameSpace.ToFilePath, _rootSchema.Name + ".schema.json"); } return(_codeGenerator.LookupReferencedPart(_codeGenerator.TransformToCamelCase(relativeLocalFileWithExpandedDot), jSBRef)); }
public void BuildReference_Absolute_WithFragment() { string refName = "refName"; string refDesc = "Description of refName"; Uri uri = new Uri("///E:/Projects/Funny/Onion/address.schema.json#/definitions/ftp", UriKind.Relative); var varref = new JSBRef(refName, refDesc, uri); JsonSchema varSchema = varref.AsJsonSchema(); }
private string GenerateDefaultIfExisting(IdentifierString key, JSBRef jsonSchemaBuilderUriReference) { if (jsonSchemaBuilderUriReference.DefaultValue != null) { throw new NotImplementedException($"Default value for Reference is not implemented"); } else { return(string.Empty); } }
protected override JSBSchema BuildJsonSchema() { if (Uri.TryCreate("./dateAsTopPart", UriKind.RelativeOrAbsolute, out Uri uri)) { JSBRef uriReferencePart = new JSBRef("MyTopPartUriReference", "TopPart", iriReference: uri); return(new JSBSchema("UriReferencesATopPart", Description, topPart: uriReferencePart)); } else { throw new Exception("Uri not valid"); } }
private void DoReferenceResolution(IJSBPart jsonSchemaBuilderPart, NamespaceString module) { switch (jsonSchemaBuilderPart.PartType) { case JSBPartType.IriReference: JSBRef jsonSchemaBuilderIriReference = jsonSchemaBuilderPart as JSBRef; if (!jsonSchemaBuilderIriReference.IsFragmentOnly) { string relativeLocalFileWithExpandedDot = jsonSchemaBuilderIriReference.RelativeLocalFile; if (relativeLocalFileWithExpandedDot.StartsWith("./") || relativeLocalFileWithExpandedDot.StartsWith(".\\")) { relativeLocalFileWithExpandedDot = Path.Combine(module.ToFilePath, relativeLocalFileWithExpandedDot.Remove(0, 2)); } string localFile = Path.Combine(JsonSchemaApplicationRoot, TransformToCamelCase(relativeLocalFileWithExpandedDot)); string uriWithoutFragmentString = jsonSchemaBuilderIriReference.IriReference.OriginalString; if (!string.IsNullOrWhiteSpace(jsonSchemaBuilderIriReference.Fragment)) { uriWithoutFragmentString = uriWithoutFragmentString.Replace("#" + jsonSchemaBuilderIriReference.Fragment, ""); } if (Uri.TryCreate(uriWithoutFragmentString, UriKind.RelativeOrAbsolute, out Uri uriWithoutFragment)) { if (!File.Exists(localFile)) { if (jsonSchemaBuilderIriReference.IriReference.IsAbsoluteUri && !jsonSchemaBuilderIriReference.IriReference.IsLoopback) { try { FileInfo fileInfo = new FileInfo(localFile); string directory = fileInfo.Directory.FullName; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } using (WebClient myWebClient = new WebClient()) { // Download the Web resource and save it into the current filesystem folder. myWebClient.DownloadFile(uriWithoutFragment, localFile); //TODO generate builderparts from schema and register in _registeredJsonSchemas } } catch (Exception ex) { throw new CodeGenerationException($"Resource at {uriWithoutFragment} could not be downloaded", ex); } } else { throw new CodeGenerationException($"Iri reference {uriWithoutFragment} was not found locally at {localFile}"); } } } } break; case JSBPartType.Schema: JSBSchema jsonSchemaBuilderSchema = jsonSchemaBuilderPart as JSBSchema; foreach (IJSBPart definition in jsonSchemaBuilderSchema.Definitions.Values) { DoReferenceResolution(definition, module); } if (jsonSchemaBuilderSchema.TopPart != null) { DoReferenceResolution(jsonSchemaBuilderSchema.TopPart, module); } break; case JSBPartType.Array: JSBArray jsonSchemaBuilderArray = jsonSchemaBuilderPart as JSBArray; foreach (IJSBPart part in jsonSchemaBuilderArray.Items) { DoReferenceResolution(part, module); } break; case JSBPartType.Object: JSBObject jsonSchemaBuilderObject = jsonSchemaBuilderPart as JSBObject; foreach (IJSBPart property in jsonSchemaBuilderObject.Properties.Values) { DoReferenceResolution(property, module); } break; default: break; } }
/// <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"); } }
private void GenerateCodeFromSchema(CodeBuilder codeBuilder, JsonValue jsonValueOfSchema, IdentifierString key, JSBRef jsonSchemaBuilderUriReference) { if (jsonValueOfSchema.Type == JsonValueType.Object) { GenerateComments(codeBuilder, key, jsonSchemaBuilderUriReference); codeBuilder .L($"[JsonProperty(\"{TransformToCamelCase(key)}\")]") .L($"public {MakeCorrectItemType(jsonValueOfSchema)} {TransformToTitleCase(key)} {{ get; set; }}{GenerateDefaultIfExisting(key, jsonSchemaBuilderUriReference)}") .EmptyLine(); } else { throw new CodeGenerationException($"GenerateOrdinaryUriReference {key} references an invalid schema {jsonSchemaBuilderUriReference.IriReference.OriginalString}"); } }
private void GenerateCodeFromInternalPart(CodeBuilder codeBuilder, IJSBPart referencedPart, IdentifierString key, JSBRef jsonSchemaBuilderUriReference) { GenerateComments(codeBuilder, key, jsonSchemaBuilderUriReference); codeBuilder .L($"[JsonProperty(\"{TransformToCamelCase(key)}\")]") .L($"public {MakeCorrectItemType(referencedPart)} {TransformToTitleCase(key)} {{ get; set; }}{GenerateDefaultIfExisting(key, jsonSchemaBuilderUriReference)}") .EmptyLine(); }
private void GenerateOrdinaryUriReference(CodeBuilder codeBuilder, IdentifierString key, JSBRef jsonSchemaBuilderIriReference) { try { (IJSBPart refPart, JsonValue schemaValue)pair = LookupReferencedPart(jsonSchemaBuilderIriReference); if (pair.refPart != null) { GenerateCodeFromInternalPart(codeBuilder, pair.refPart, key, jsonSchemaBuilderIriReference); } else if (pair.schemaValue != null) { GenerateCodeFromSchema(codeBuilder, pair.schemaValue, key, jsonSchemaBuilderIriReference); } else { throw new CodeGenerationException($"LookupReferencedPart failed. Might be because Uri reference is not supported other than /definitions/ or entire schema"); } } catch (CodeGenerationException) { throw; } catch (Exception ex) { throw new CodeGenerationException($"GenerateOrdinaryUriReference failed for some reason", ex); } }
private string MakeCorrectItemType(IJSBPart jsonSchemaBuilderPart) { switch (jsonSchemaBuilderPart.PartType) { case JSBPartType.Array: //TODO support multiple items return($"List<{MakeCorrectItemType((jsonSchemaBuilderPart as JSBArray).Items.First())}>"); case JSBPartType.Object: return(jsonSchemaBuilderPart.Name); case JSBPartType.Integer: return("long"); case JSBPartType.Number: return("double"); case JSBPartType.Boolean: return("bool"); case JSBPartType.String: JSBString jSBString = jsonSchemaBuilderPart as JSBString; if (jSBString.Enums.Count > 0) { return(jsonSchemaBuilderPart.Name + "Enum"); } return("string"); case JSBPartType.Date: return("DateTime"); case JSBPartType.DateTime: return("DateTime"); case JSBPartType.Email: return("Email"); case JSBPartType.Time: return("DateTime"); case JSBPartType.Schema: JSBSchema jSBSchema = jsonSchemaBuilderPart as JSBSchema; if (jSBSchema.TopPart != null) { return(MakeCorrectItemType(jSBSchema.TopPart)); } else { throw new NotImplementedException($"MakeCorrectItemType for Schema without a topPart has not been implemented"); } case JSBPartType.IriReference: JSBRef jsbRef = jsonSchemaBuilderPart as JSBRef; var pair = LookupReferencedPart(jsbRef); if (pair.refPart != null) { return(MakeCorrectItemType(pair.refPart)); } else if (pair.schemaValue != null) { return(MakeCorrectItemType(pair.schemaValue)); } else { throw new CodeGenerationException($"Could not find reference to {jsbRef.RelativeLocalFile}"); } default: throw new NotImplementedException($"MakeCorrectItemType for {jsonSchemaBuilderPart.PartType} has not been implemented"); } }
private void GenerateCodeForBuilderPart(CodeBuilder codeBuilder, IdentifierString key, IJSBPart value, Dictionary <string, IJSBPart> definitions = null, bool parentIsArray = false, bool isDefinition = false) { switch (value.PartType) { case JSBPartType.Array: JSBArray jsonSchemaBuilderArray = value as JSBArray; if (jsonSchemaBuilderArray.Enums != null && jsonSchemaBuilderArray.Enums.Count > 0) { GenerateEnumArray(codeBuilder, key, jsonSchemaBuilderArray); } else { GenerateOrdinaryArray(codeBuilder, key, jsonSchemaBuilderArray); } break; case JSBPartType.Boolean: JSBBoolean jsonSchemaBuilderBoolean = value as JSBBoolean; if (jsonSchemaBuilderBoolean.Enums != null && jsonSchemaBuilderBoolean.Enums.Count > 0) { GenerateEnumBoolean(codeBuilder, key, jsonSchemaBuilderBoolean); } else { if (!parentIsArray) { GenerateOrdinaryBoolaen(codeBuilder, key, jsonSchemaBuilderBoolean); } } break; case JSBPartType.Date: JSBDate jsonSchemaBuilderDate = value as JSBDate; if (jsonSchemaBuilderDate.Enums != null && jsonSchemaBuilderDate.Enums.Count > 0) { GenerateEnumDate(codeBuilder, key, jsonSchemaBuilderDate); } else { if (!parentIsArray) { GenerateOrdinaryDate(codeBuilder, key, jsonSchemaBuilderDate); } } break; case JSBPartType.DateTime: JSBDateTime jsonSchemaBuilderDateTime = value as JSBDateTime; if (jsonSchemaBuilderDateTime.Enums != null && jsonSchemaBuilderDateTime.Enums.Count > 0) { GenerateEnumDateTime(codeBuilder, key, jsonSchemaBuilderDateTime); } else { if (!parentIsArray) { GenerateOrdinaryDateTime(codeBuilder, key, jsonSchemaBuilderDateTime); } } break; case JSBPartType.Email: JSBEmail jsonSchemaBuilderEmail = value as JSBEmail; if (jsonSchemaBuilderEmail.Enums != null && jsonSchemaBuilderEmail.Enums.Count > 0) { GenerateEnumEmail(codeBuilder, key, jsonSchemaBuilderEmail); } else { GenerateOrdinaryEmail(codeBuilder, key, jsonSchemaBuilderEmail); } break; case JSBPartType.Integer: JSBInteger jsonSchemaBuilderInteger = value as JSBInteger; if (jsonSchemaBuilderInteger.Enums != null && jsonSchemaBuilderInteger.Enums.Count > 0) { GenerateEnumInteger(codeBuilder, key, jsonSchemaBuilderInteger); } else { if (!parentIsArray) { GenerateOrdinaryInteger(codeBuilder, key, jsonSchemaBuilderInteger); } } break; case JSBPartType.Number: JSBNumber jsonSchemaBuilderNumber = value as JSBNumber; if (jsonSchemaBuilderNumber.Enums != null && jsonSchemaBuilderNumber.Enums.Count > 0) { GenerateEnumNumber(codeBuilder, key, jsonSchemaBuilderNumber); } else { if (!parentIsArray) { GenerateOrdinaryNumber(codeBuilder, key, jsonSchemaBuilderNumber); } } break; case JSBPartType.Object: JSBObject jsonSchemaBuilderObject = value as JSBObject; if (jsonSchemaBuilderObject.Enums != null && jsonSchemaBuilderObject.Enums.Count > 0) { GenerateEnumObject(codeBuilder, key, jsonSchemaBuilderObject, definitions); } else { GenerateOrdinaryObject(codeBuilder, key, jsonSchemaBuilderObject, definitions, parentIsArray); } break; case JSBPartType.String: JSBString jsonSchemaBuilderString = value as JSBString; if (jsonSchemaBuilderString.Enums != null && jsonSchemaBuilderString.Enums.Count > 0) { GenerateEnumString(codeBuilder, key, jsonSchemaBuilderString, isDefinition); } else { GenerateOrdinaryString(codeBuilder, key, jsonSchemaBuilderString); } break; case JSBPartType.Time: JSBTime jsonSchemaBuilderTime = value as JSBTime; if (jsonSchemaBuilderTime.Enums != null && jsonSchemaBuilderTime.Enums.Count > 0) { GenerateEnumTime(codeBuilder, key, jsonSchemaBuilderTime); } else { GenerateOrdinaryTime(codeBuilder, key, jsonSchemaBuilderTime); } break; case JSBPartType.IriReference: //Reference cannot be an enum if (!parentIsArray) { JSBRef jsonSchemaBuilderUriReference = value as JSBRef; GenerateOrdinaryUriReference(codeBuilder, key, jsonSchemaBuilderUriReference); } break; default: codeBuilder.L($"throw new NotImplementedException(\"PartType {value.PartType} is not implemented\");"); break; } }