/// <summary> /// Builds a JSON schema based on the uploaded XSD. /// </summary> /// <remarks> /// This operation is using the new data modelling library. /// </remarks> /// <param name="org">Organization owning the repository identified by it's short name.</param> /// <param name="repository">Repository name to search for schema files.</param> /// <param name="developer">Developers short name</param> /// <param name="fileName">The name of the file being uploaded.</param> /// <param name="xsdStream">Stream representing the XSD.</param> public async Task <string> BuildSchemaFromXsd( string org, string repository, string developer, string fileName, Stream xsdStream) { var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository( org, repository, developer); MemoryStream xsdMemoryStream = new MemoryStream(); xsdStream.CopyTo(xsdMemoryStream); AltinnRepositoryType altinnRepositoryType = await altinnAppGitRepository.GetRepositoryType(); if (altinnRepositoryType == AltinnRepositoryType.Datamodels) { xsdMemoryStream.Position = 0; Json.Schema.JsonSchema jsonSchema = GenerateJsonSchemaFromXsd(xsdMemoryStream); var jsonContent = SerializeJson(jsonSchema); await altinnAppGitRepository.WriteTextByRelativePathAsync( Path.ChangeExtension(fileName, "schema.json"), jsonContent, true); return(jsonContent); } /* From here repository is assumed to be for an app. Validate with a Directory.Exist check? */ string filePath = Path.Combine(altinnAppGitRepository.GetRelativeModelFolder(), fileName); DatamodellingPreference datamodellingPreference = await altinnAppGitRepository.GetDatamodellingPreference(); switch (datamodellingPreference) { case DatamodellingPreference.JsonSchema: /* Using the NEW model processing. */ xsdMemoryStream.Position = 0; await SaveOriginalXsd(org, repository, developer, filePath, xsdMemoryStream); xsdMemoryStream.Position = 0; string jsonContent = await ProcessNewXsd(altinnAppGitRepository, xsdMemoryStream, filePath); return(jsonContent); case DatamodellingPreference.Xsd: default: /* Using the OLD model processing. */ xsdMemoryStream.Position = 0; Manatee.Json.Schema.JsonSchema schemaJsonSchema = GenerateJsonSchema(xsdMemoryStream); string jsonSerialized = SerializeJson(schemaJsonSchema); await UpdateAllAppModelFiles(org, repository, developer, Path.ChangeExtension(filePath, "schema.json"), jsonSerialized); return(jsonSerialized); } }
public async Task <ActionResult <RepositoryModel> > CreateApp(string org, [FromQuery] string repository, [FromQuery] DatamodellingPreference datamodellingPreference = DatamodellingPreference.JsonSchema) { try { Guard.AssertValidAppRepoName(repository); } catch (ArgumentException) { return(BadRequest($"{repository} is an invalid repository name.")); } if (datamodellingPreference == DatamodellingPreference.Unknown) { datamodellingPreference = DatamodellingPreference.JsonSchema; } var config = new ServiceConfiguration { RepositoryName = repository, ServiceName = repository, DatamodellingPreference = datamodellingPreference }; var repositoryResult = await _repository.CreateService(org, config); if (repositoryResult.RepositoryCreatedStatus == HttpStatusCode.Created) { return(Created(repositoryResult.CloneUrl, repositoryResult)); } else { return(StatusCode((int)repositoryResult.RepositoryCreatedStatus, repositoryResult)); } }