-
Notifications
You must be signed in to change notification settings - Fork 289
Add schema and serialization support for autoentities wildcard enhancement #2973
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
81330ac
7cfbd5d
15e4674
92fe8a2
e02c53d
6cbba87
8c25493
fd4df81
db3d137
a443ae0
853e356
30ac9d6
48c48f4
1dcfbed
7be3a41
81cf6b1
ed8e1c0
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,111 @@ | ||||||||||||||||||
| // Copyright (c) Microsoft Corporation. | ||||||||||||||||||
| // Licensed under the MIT License. | ||||||||||||||||||
|
|
||||||||||||||||||
| using System.Text.Json; | ||||||||||||||||||
| using System.Text.Json.Serialization; | ||||||||||||||||||
| using Azure.DataApiBuilder.Config.ObjectModel; | ||||||||||||||||||
|
|
||||||||||||||||||
| namespace Azure.DataApiBuilder.Config.Converters; | ||||||||||||||||||
|
|
||||||||||||||||||
| internal class AutoentityConverter : JsonConverter<Autoentity> | ||||||||||||||||||
| { | ||||||||||||||||||
| // Settings for variable replacement during deserialization. | ||||||||||||||||||
| private readonly DeserializationVariableReplacementSettings? _replacementSettings; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// <param name="replacementSettings">Settings for variable replacement during deserialization. | ||||||||||||||||||
| /// If null, no variable replacement will be performed.</param> | ||||||||||||||||||
| public AutoentityConverter(DeserializationVariableReplacementSettings? replacementSettings = null) | ||||||||||||||||||
| { | ||||||||||||||||||
| _replacementSettings = replacementSettings; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// <inheritdoc/> | ||||||||||||||||||
| public override Autoentity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||||||||||||||||||
| { | ||||||||||||||||||
| if (reader.TokenType is JsonTokenType.StartObject) | ||||||||||||||||||
| { | ||||||||||||||||||
| // Initialize all sub-properties to null. | ||||||||||||||||||
| AutoentityPatterns? patterns = null; | ||||||||||||||||||
| AutoentityTemplate? template = null; | ||||||||||||||||||
| EntityPermission[]? permissions = null; | ||||||||||||||||||
|
|
||||||||||||||||||
| while (reader.Read()) | ||||||||||||||||||
| { | ||||||||||||||||||
| if (reader.TokenType == JsonTokenType.EndObject) | ||||||||||||||||||
| { | ||||||||||||||||||
| return new Autoentity(patterns, template, permissions); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| string? propertyName = reader.GetString(); | ||||||||||||||||||
|
|
||||||||||||||||||
| reader.Read(); | ||||||||||||||||||
| switch (propertyName) | ||||||||||||||||||
| { | ||||||||||||||||||
| case "patterns": | ||||||||||||||||||
| AutoentityPatternsConverter patternsConverter = new(_replacementSettings); | ||||||||||||||||||
| patterns = patternsConverter.Read(ref reader, typeof(AutoentityPatterns), options); | ||||||||||||||||||
| break; | ||||||||||||||||||
|
|
||||||||||||||||||
| case "template": | ||||||||||||||||||
| AutoentityTemplateConverter templateConverter = new(_replacementSettings); | ||||||||||||||||||
| template = templateConverter.Read(ref reader, typeof(AutoentityTemplate), options); | ||||||||||||||||||
| break; | ||||||||||||||||||
|
|
||||||||||||||||||
| case "permissions": | ||||||||||||||||||
| permissions = JsonSerializer.Deserialize<EntityPermission[]>(ref reader, options); | ||||||||||||||||||
| break; | ||||||||||||||||||
|
|
||||||||||||||||||
| default: | ||||||||||||||||||
| throw new JsonException($"Unexpected property {propertyName}"); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| throw new JsonException("Unable to read the Autoentities"); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// <summary> | ||||||||||||||||||
| /// When writing the autoentities back to a JSON file, only write the properties | ||||||||||||||||||
| /// if they are user provided. This avoids polluting the written JSON file with properties | ||||||||||||||||||
| /// the user most likely omitted when writing the original DAB runtime config file. | ||||||||||||||||||
| /// This Write operation is only used when a RuntimeConfig object is serialized to JSON. | ||||||||||||||||||
| /// </summary> | ||||||||||||||||||
| /// <inheritdoc/> | ||||||||||||||||||
| public override void Write(Utf8JsonWriter writer, Autoentity value, JsonSerializerOptions options) | ||||||||||||||||||
| { | ||||||||||||||||||
| writer.WriteStartObject(); | ||||||||||||||||||
|
|
||||||||||||||||||
| AutoentityPatterns? patterns = value?.Patterns; | ||||||||||||||||||
| if (patterns?.UserProvidedIncludeOptions is true | ||||||||||||||||||
| || patterns?.UserProvidedExcludeOptions is true | ||||||||||||||||||
| || patterns?.UserProvidedNameOptions is true) | ||||||||||||||||||
| { | ||||||||||||||||||
| AutoentityPatternsConverter autoentityPatternsConverter = options.GetConverter(typeof(AutoentityPatterns)) as AutoentityPatternsConverter ?? | ||||||||||||||||||
| throw new JsonException("Failed to get autoentities.patterns options converter"); | ||||||||||||||||||
| writer.WritePropertyName("patterns"); | ||||||||||||||||||
| autoentityPatternsConverter.Write(writer, patterns, options); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| AutoentityTemplate? template = value?.Template; | ||||||||||||||||||
| if (template?.UserProvidedMcpOptions is true | ||||||||||||||||||
| || template?.UserProvidedRestOptions is true | ||||||||||||||||||
| || template?.UserProvidedGraphQLOptions is true | ||||||||||||||||||
| || template?.UserProvidedHealthOptions is true | ||||||||||||||||||
| || template?.UserProvidedCacheOptions is true) | ||||||||||||||||||
| { | ||||||||||||||||||
| AutoentityTemplateConverter autoentityTemplateConverter = options.GetConverter(typeof(AutoentityTemplate)) as AutoentityTemplateConverter ?? | ||||||||||||||||||
| throw new JsonException("Failed to get autoentities.template options converter"); | ||||||||||||||||||
| writer.WritePropertyName("template"); | ||||||||||||||||||
| autoentityTemplateConverter.Write(writer, template, options); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
||||||||||||||||||
| // Serialize permissions if user provided | |
| if (value?.UserProvidedPermissionsOptions is true) | |
| { | |
| writer.WritePropertyName("permissions"); | |
| JsonSerializer.Serialize(writer, value.Permissions, options); | |
| } |
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.
again, a valid suggestion
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.
The
AutoentityConverterRead method only handles "patterns" and "template" properties but doesn't handle the "permissions" property. The permissions are defined in theAutoentityclass (line 19) and in the JSON schema, but they are never deserialized from JSON. Add a case to handle "permissions" property deserialization.