-
Notifications
You must be signed in to change notification settings - Fork 24
Add utilites for reading using xsd files from the models #1615
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -523,4 +523,20 @@ | |
|
|
||
| return filedata; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string? GetXsdSchema(string dataTypeId) | ||
| { | ||
| string legalPath = Path.Join(_settings.AppBasePath, _settings.ModelsFolder); | ||
| string filename = Path.Join(legalPath, $"{dataTypeId}.xsd"); | ||
| PathHelper.EnsureLegalPath(legalPath, filename); | ||
|
|
||
| string? filedata = null; | ||
| if (File.Exists(filename)) | ||
|
||
| { | ||
| filedata = File.ReadAllText(filename, Encoding.UTF8); | ||
| } | ||
|
|
||
| return filedata; | ||
| } | ||
|
Comment on lines
+527
to
+541
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add telemetry to match the established pattern. The using var activity = _telemetry?.StartGetValidationConfigurationActivity();Consider adding similar telemetry to track XSD schema retrieval operations. 📊 Proposed addition /// <inheritdoc />
public string? GetXsdSchema(string dataTypeId)
{
+ using var activity = _telemetry?.StartGetXsdSchemaActivity();
string legalPath = Path.Join(_settings.AppBasePath, _settings.ModelsFolder);
string filename = Path.Join(legalPath, $"{dataTypeId}.xsd");
PathHelper.EnsureLegalPath(legalPath, filename);
string? filedata = null;
if (File.Exists(filename))
{
filedata = File.ReadAllText(filename, Encoding.UTF8);
}
return filedata;
}Note: You'll also need to add the corresponding telemetry activity method to the Telemetry class. 🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Altinn.App.Api.Tests.Controllers; | ||
|
|
||
| public class ResourceControllerTests : ApiTestBase, IClassFixture<WebApplicationFactory<Program>> | ||
| { | ||
| public ResourceControllerTests(WebApplicationFactory<Program> factory, ITestOutputHelper outputHelper) | ||
| : base(factory, outputHelper) { } | ||
|
|
||
| private const string Org = "tdd"; | ||
| private const string App = "contributer-restriction"; | ||
| private const string DefaultDataTypeId = "Skjema"; | ||
|
|
||
| [Fact] | ||
| public async Task GetModelJsonSchema_ReturnsOk() | ||
| { | ||
| var client = GetRootedClient(Org, App); | ||
| using var response = await client.GetAsync($"/{Org}/{App}/api/jsonschema/{DefaultDataTypeId}"); | ||
| response.EnsureSuccessStatusCode(); | ||
| var content = await response.Content.ReadAsStringAsync(); | ||
| Assert.NotNull(content); | ||
| Assert.Contains("\"$comment\"", content); // TODO: Update when the schema in the test project has more content. | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetModelJsonSchema_InvalidDataTypeId_ReturnsNotFound() | ||
| { | ||
| var client = GetRootedClient(Org, App); | ||
| await Assert.ThrowsAsync<FileNotFoundException>(async () => | ||
| { | ||
| using var response = await client.GetAsync($"/{Org}/{App}/api/jsonschema/InvalidDataTypeId"); | ||
| // TODO: Should probably return 404 NotFound instead of throwing FileNotFoundException, | ||
| // but adding test for current behaviour. | ||
| // Assert.Equal(System.Net.HttpStatusCode.NotFound, response.StatusCode); | ||
| }); | ||
| } | ||
|
Comment on lines
+26
to
+37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test name doesn't match the behavior being tested. The test is named 📝 Suggested test name update-public async Task GetModelJsonSchema_InvalidDataTypeId_ReturnsNotFound()
+public async Task GetModelJsonSchema_InvalidDataTypeId_ThrowsFileNotFoundException()Note: The TODO comment correctly identifies that the endpoint should return 404 NotFound instead of throwing an exception. This could be addressed in a follow-up improvement to align with the 🤖 Prompt for AI Agents |
||
|
|
||
| [Fact] | ||
| public async Task GetXmlSchema_ReturnsOk() | ||
| { | ||
| var client = GetRootedClient(Org, App); | ||
| using var response = await client.GetAsync($"/{Org}/{App}/api/xsdschema/{DefaultDataTypeId}"); | ||
| response.EnsureSuccessStatusCode(); | ||
| var content = await response.Content.ReadAsStringAsync(); | ||
| Assert.NotNull(content); | ||
| Assert.Contains("<xs:schema xmlns:xs=", content); // TODO: Update when the schema in the test project has more content. | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetXmlSchema_InvalidDataTypeId_ReturnsNotFound() | ||
| { | ||
| var client = GetRootedClient(Org, App); | ||
| using var response = await client.GetAsync($"/{Org}/{App}/api/xsdschema/InvalidDataTypeId"); | ||
| Assert.Equal(System.Net.HttpStatusCode.NotFound, response.StatusCode); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "$comment": "TODO: ensure content if required for test" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- TODO: make sure this is in sync with actual model--> | ||
| <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> | ||
| </xs:schema> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing parameter documentation.
The XML documentation is missing the
<param name="id">tag and could benefit from a<returns>tag explaining the 200 vs 404 responses.📝 Suggested documentation improvement
Note: This endpoint properly returns 404 for missing schemas, which is an improvement over
GetModelJsonSchema(lines 24-36) that throwsFileNotFoundException. Consider aligning the error handling ofGetModelJsonSchemawith this approach in a future update.🤖 Prompt for AI Agents