Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

Emitter customization with field default value not reflected in fromJson method #3220

Closed

Description

Description

I am renaming the variant of an enum and then adjusting the default value of a different class that uses this variant using the following customization code:

        customization.getClass("com.azure.ai.agents.models", "ComputerActionType").customizeAst(ast -> ast.getEnumByName("ComputerActionType")
                .ifPresent(clazz -> clazz.getEntries().stream()
                        .filter(entry -> "KEYPRESS".equals(entry.getName().getIdentifier()))
                        .forEach(entry -> entry.setName("KEY_PRESS"))));

        customization.getClass("com.azure.ai.agents.models", "ComputerActionKeyPress")
                .customizeAst(ast -> ast.getClassByName("ComputerActionKeyPress")
                        .flatMap(clazz -> clazz.getFieldByName("type"))
                        .ifPresent(barField ->
                                barField.getVariables().forEach(var -> {
                                    if (var.getNameAsString().equals("type")) {
                                        var.setInitializer("ComputerActionType.KEY_PRESS");
                                    }
                                })));

The generated code works almost as expected with the exception of how the deserialization method fromJson gets generated. I get the following:

@Immutable
public final class ComputerActionKeyPress extends ComputerAction {

    /*
     * The type property.
     */
    @Generated
    private ComputerActionType type = ComputerActionType.KEY_PRESS;

// ...

   @Generated
    public static ComputerActionKeyPress fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            List<String> keys = null;
            ComputerActionType type = ComputerActionType.KEYPRESS;
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();
                if ("keys".equals(fieldName)) {
                    keys = reader.readArray(reader1 -> reader1.getString());
                    // ....

// ...

Expectation

The initialized value in the fromJson method also matches the new default value set in the customization.

Activity

  1. weidongxu-microsoft commented on Nov 12, 2025

    @weidongxu-microsoft
    Member

    The customizeAst is actually a pretty low level "modification" on the code (AST means Abstract Syntax Tree).

    Basically, this means you are directly changing the code (in this case, the initializer of ComputerActionType.KEY_PRESS), and that code alone. Nothing would be changed anywhere else.

    So, if you use customizeAst, you would need to modify the fromJson as well.

  2. weidongxu-microsoft commented on Nov 12, 2025

    @weidongxu-microsoft
    Member

    BTW, after we moved away from Eclipse language server, we now only have customizeAst. Is it correct? Alan Zimmer (@alzimmermsft)

  3. alzimmermsft commented on Nov 12, 2025

    @alzimmermsft
    Member

    All of what Weidong Xu (@weidongxu-microsoft) said is correct.

    Code customizations now only have a basic AST customization API which is on ClassCustomization and when using it if multiple spots in code use the modified code, field name change, method name change, etc, all usages will need to be modified manually. This was done as we should be preferring Swagger transforms and TypeSpec language customizations as those can be shared by all consumers of said Swagger and TypeSpec, whereas code customizations are specific to our code generation.

  4. jpalvarezl commented on Nov 12, 2025

    @jpalvarezl
    MemberAuthor

    Thank you for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions