Skip to content

Commit c8d418a

Browse files
committed
Improve null-safety, error logging, and minor optimizations
- Add null-forgiving operator for schemaNode in JsonSchemaValidator - Enhance error logging with exception details in JsonSchemaParser - Optimize property checks and retrieval in JsonSchemaParser - Remove unused dataQuery serialization in SubmodelController
1 parent 6b48418 commit c8d418a

3 files changed

Lines changed: 5 additions & 9 deletions

File tree

source/AAS.TwinEngine.DataEngine/Infrastructure/Providers/PluginDataProvider/Helper/JsonSchemaValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void ValidateRequestSchema(JsonSchema schema)
4545

4646
try
4747
{
48-
var jsonElement = JsonDocument.Parse(schemaNode.ToJsonString()).RootElement;
48+
var jsonElement = JsonDocument.Parse(schemaNode!.ToJsonString()).RootElement;
4949

5050
var result = MetaSchemas.Draft202012.Evaluate(jsonElement, _evaluationOptions);
5151

source/AAS.TwinEngine.Plugin.TestPlugin/Api/Submodel/Services/JsonSchemaParser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ private void ValidateRequest(JsonSchema jsonSchema)
3535
throw new BadRequestException(ExceptionMessages.RequestBodyInvalid);
3636
}
3737
}
38-
catch (JsonException)
38+
catch (JsonException ex)
3939
{
40-
logger.LogError("Requested schema is not valid");
40+
logger.LogError(ex, "Requested schema is not valid");
4141
throw new BadRequestException(ExceptionMessages.FailedParsingJsonSchema);
4242
}
4343
}
@@ -48,12 +48,12 @@ private SemanticTreeNode CreateSemanticTree(JsonSchema jsonSchema)
4848

4949
if (!json.TryGetPropertyValue("properties", out var propsNode) ||
5050
propsNode is not JsonObject props ||
51-
!props.Any())
51+
props.Count == 0)
5252
{
5353
throw new BadRequestException(ExceptionMessages.InvalidJsonSchemaRootElement);
5454
}
5555

56-
var rootProperty = props.First();
56+
var rootProperty = ((IList<KeyValuePair<string, JsonNode?>>)props)[0];
5757
return ProcessProperty(rootProperty.Key, rootProperty.Value!, json);
5858
}
5959

source/AAS.TwinEngine.Plugin.TestPlugin/Api/Submodel/SubmodelController.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ public class SubmodelController(ISubmodelHandler submodelHandler) : ControllerBa
2626
[ProducesResponseType(typeof(ActionResult), StatusCodes.Status500InternalServerError)]
2727
public async Task<ActionResult<JsonObject>> RetrieveDataAsync([FromBody] JsonSchema? dataQuery, [FromRoute] string submodelId, CancellationToken cancellationToken)
2828
{
29-
var schemaJsonString = JsonSerializer.Serialize(dataQuery, new JsonSerializerOptions
30-
{
31-
WriteIndented = true // Optional: for human-readable output
32-
});
3329
var decodedSubmodelId = submodelId.DecodeBase64();
3430
if (dataQuery is null)
3531
{

0 commit comments

Comments
 (0)