Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete modelMetadata on model updates #12655

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.DataModeling.Metamodel;
using Altinn.Studio.Designer.Configuration;
using Altinn.Studio.Designer.Exceptions.AppDevelopment;
using Altinn.Studio.Designer.Helpers;
Expand Down Expand Up @@ -142,30 +141,16 @@ public async Task<ServiceConfiguration> GetAppMetadataConfig()
}

/// <summary>
/// Gets the model metadata content based on model name. If no model metadata found for the model name an empty model metadata is returned.
/// Deletes model metadata file as it is generated on the fly and does not need to be in the repo.
/// </summary>
/// <param name="modelName">The model metadata as string</param>
public async Task<string> GetModelMetadata(string modelName)
/// <param name="modelMetadataFilePath">The full relative path to the model metadata</param>
public void DeleteModelMetadata(string modelMetadataFilePath)
{
string modelMetadataFileName = GetPathToModelMetadata(modelName);
if (!FileExistsByRelativePath(modelMetadataFileName))
if (FileExistsByRelativePath(modelMetadataFilePath))
{
ModelMetadata emptyModel = JsonSerializer.Deserialize<ModelMetadata>("{}");
return JsonSerializer.Serialize(emptyModel);
string absolutePath = GetAbsoluteFileOrDirectoryPathSanitized(modelMetadataFilePath);
File.Delete(absolutePath);
}
return await ReadTextByRelativePathAsync(modelMetadataFileName);
}

/// <summary>
/// Saves the model metadata model for the application (a JSON where the model hierarchy is flatten,
/// in order to easier generate the C# class) to disk.
/// </summary>
/// <param name="modelMetadata">Model metadata to persist.</param>
/// <param name="modelName">The name of the model. </param>
public async Task SaveModelMetadata(string modelMetadata, string modelName)
{
string modelMetadataRelativeFilePath = GetPathToModelMetadata(modelName);
await WriteTextByRelativePathAsync(modelMetadataRelativeFilePath, modelMetadata, true);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,25 @@ public async Task<string> GetSchema(AltinnRepoEditingContext altinnRepoEditingCo
public async Task UpdateSchema(AltinnRepoEditingContext altinnRepoEditingContext, string relativeFilePath, string jsonContent, bool saveOnly = false, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var altinnGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(altinnRepoEditingContext.Org, altinnRepoEditingContext.Repo, altinnRepoEditingContext.Developer);
var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(altinnRepoEditingContext.Org, altinnRepoEditingContext.Repo, altinnRepoEditingContext.Developer);
var jsonSchema = Json.Schema.JsonSchema.FromText(jsonContent);
var serializedJsonContent = SerializeJson(jsonSchema);
altinnAppGitRepository.DeleteModelMetadata(relativeFilePath.Replace(".schema.json", ".metadata.json"));
if (saveOnly)
{
// Only save updated JSON schema - no model file generation
await altinnGitRepository.WriteTextByRelativePathAsync(relativeFilePath, serializedJsonContent, true, cancellationToken);
await altinnAppGitRepository.WriteTextByRelativePathAsync(relativeFilePath, serializedJsonContent, true, cancellationToken);
return;
}

var repositoryType = await altinnGitRepository.GetRepositoryType();
var repositoryType = await altinnAppGitRepository.GetRepositoryType();

if (repositoryType == AltinnRepositoryType.Datamodels)
{
// Datamodels repository - save JSON and update XSD
await altinnGitRepository.WriteTextByRelativePathAsync(relativeFilePath, serializedJsonContent, true, cancellationToken);
await altinnAppGitRepository.WriteTextByRelativePathAsync(relativeFilePath, serializedJsonContent, true, cancellationToken);
XmlSchema xsd = _jsonSchemaToXmlSchemaConverter.Convert(jsonSchema);
await altinnGitRepository.SaveXsd(xsd, relativeFilePath.Replace(".schema.json", ".xsd"));
await altinnAppGitRepository.SaveXsd(xsd, relativeFilePath.Replace(".schema.json", ".xsd"));
return;
}

Expand Down
59 changes: 59 additions & 0 deletions backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,65 @@ public async Task UpdateSchema_AppRepo_ShouldUpdate()
}
}

[Fact]
public async Task UpdateSchema_ModelMetadataExistForModelInRepo_ShouldDeleteModelMetadata()
{
// Arrange
var org = "ttd";
var sourceRepository = "hvem-er-hvem";
var developer = "testUser";
var targetRepository = TestDataHelper.GenerateTestRepoName();
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, targetRepository, developer);

await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);
try
{
var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory());
ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, TestDataHelper.ModelMetadataToCsharpConverter);
var updatedSchema = @"{""properties"":{""rootType1"":{""$ref"":""#/definitions/rootType""}},""definitions"":{""rootType"":{""properties"":{""keyword"":{""type"":""string""}}}}}";

var altinnGitRepository = altinnGitRepositoryFactory.GetAltinnGitRepository(org, targetRepository, developer);
Assert.True(altinnGitRepository.FileExistsByRelativePath("App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.metadata.json"));
await schemaModelService.UpdateSchema(editingContext, "App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.schema.json", updatedSchema);
Assert.False(altinnGitRepository.FileExistsByRelativePath("App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.metadata.json"));
}
finally
{
TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
}
}

[Fact]
public async Task UpdateSchema_NoModelMetadataForModelInRepo_ShouldOnlyUpdate()
{
// Arrange
var org = "ttd";
var sourceRepository = "hvem-er-hvem";
var developer = "testUser";
var targetRepository = TestDataHelper.GenerateTestRepoName();
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, targetRepository, developer);

await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);
try
{
var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory());
ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, TestDataHelper.ModelMetadataToCsharpConverter);
var expectedUpdatedSchema = @"{""properties"":{""rootType1"":{""$ref"":""#/definitions/rootType""}},""definitions"":{""rootType"":{""properties"":{""keyword"":{""type"":""string""}}}}}";
var altinnGitRepository = altinnGitRepositoryFactory.GetAltinnGitRepository(org, targetRepository, developer);
Assert.False(altinnGitRepository.FileExistsByRelativePath("App/models/HvemErHvem_SERES.metadata.json"));

await schemaModelService.UpdateSchema(editingContext, "App/models/HvemErHvem_SERES.schema.json", expectedUpdatedSchema);
Assert.False(altinnGitRepository.FileExistsByRelativePath("App/models/HvemErHvem_SERES.metadata.json"));
var updatedSchema = await altinnGitRepository.ReadTextByRelativePathAsync("App/models/HvemErHvem_SERES.schema.json");
string serializedExpectedSchemaUpdates = FormatJsonString(updatedSchema);
updatedSchema.Should().BeEquivalentTo(serializedExpectedSchemaUpdates);
}
finally
{
TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
}
}

[Fact]
public async Task UpdateSchema_InvalidJsonSchema_ShouldThrowException()
{
Expand Down
Loading