Skip to content

PropertyNamingPolicy != CamelCase prevents script execution #199

@criton2000

Description

@criton2000

Hello, thank you for your excellent library.
I encountered an issue (I searched for an issue on this argument, but I did not find anything, forgive me if my failure)
It seems that configuring JsonService to serialize object properties with a rule different from CamelCase, javascript module cannot be executed.
Executing following code (substantially static API example with JsonService settings) throws a Jering.Javascript.NodeJS.InvocationException: Invalid module source type: undefined.
If PropertyNamingPolicy == JsonNamingPolicy.CamelCase it works regularily, with other values for PropertyNamingPolicy (SnakeCase* or KebabCase*) it throws Jering.Javascript.NodeJS.InvocationException: Unexpected error
Jering library version 7.0.0, .NET 8, nodeJs 20.10.0

using Jering.Javascript.NodeJS;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Text.Encodings.Web;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace testJeringNodeMinimal
{
    public class MyJsonService : IJsonService
    {
        private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
        {
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
            PropertyNamingPolicy = null, //JsonNamingPolicy.CamelCase,
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
            PropertyNameCaseInsensitive = true
        };

        public static JsonSerializerOptions SerializerOptions => _jsonSerializerOptions;
        /// <inheritdoc />
        public ValueTask<T?> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default)
        {
            return JsonSerializer.DeserializeAsync<T>(stream, _jsonSerializerOptions, cancellationToken);
        }

        /// <inheritdoc />
        public Task SerializeAsync<T>(Stream stream, T value, CancellationToken cancellationToken = default)
        {
            return JsonSerializer.SerializeAsync(stream, value, _jsonSerializerOptions, cancellationToken);
        }
    }

    internal class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            var services = new ServiceCollection();
            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.SetMinimumLevel(LogLevel.Debug);
                loggingBuilder.AddConsole();
            });
            services.AddNodeJS();

            // Overwrite the DI service
            services.AddSingleton<IJsonService, MyJsonService>();

            StaticNodeJSService.SetServices(services);
            {
                try
                {
                    string javascriptModule = @"
module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
    var result = x + y; // Your javascript logic
    callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}";

                    // Invoke javascript
                    int result = await StaticNodeJSService.InvokeFromStringAsync<int>(javascriptModule, args: new object[] { 3, 5 });
                    Console.WriteLine(result);

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    throw;
                }
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions