Skip to content

Skipped generation by interceptors leaves imports untouched #1111

@timocov

Description

@timocov

Lets say you have a CodeInterceptor implementation that skips the codegen for certain classes:

public final class Interceptor implements CodeInterceptor<ClassSection, JavaWriter> {
    private final CodeGenerationContext context;

    public Interceptor(CodeGenerationContext context) {
        this.context = context;
    }

    @Override
    public Class<ClassSection> sectionType() {
        return ClassSection.class;
    }

    @Override
    public void write(JavaWriter writer, String previousText, ClassSection section) {
        var targetedShape = section.targetedShape();
        if (targetedShape.isServiceShape() && isGenerationForSymbol(writer, context.symbolProvider().toSymbol(targetedShape))) {
            // skip codegen
            return;
        }

        writer.writeWithNoFormatting(previousText);
    }

    private static boolean isGenerationForSymbol(JavaWriter writer, Symbol symbol) {
        var filename = writer.getFilename();
        var normalizedSymbolFilename = Paths.get(symbol.getDeclarationFile()).normalize().toString();
        return normalizedSymbolFilename.equals(filename);
    }
}

When you add this interceptor to the build the file still will be generated but will just have a bunch of imports in it:

package test.service;

import java.util.List;
import java.util.function.Function;
import software.amazon.smithy.java.core.schema.Schema;
import software.amazon.smithy.java.core.schema.SchemaIndex;
import software.amazon.smithy.java.core.schema.SerializableStruct;
import software.amazon.smithy.java.core.serde.TypeRegistry;
import software.amazon.smithy.java.framework.model.UnknownOperationException;
import software.amazon.smithy.java.server.Operation;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.utils.SmithyGenerated;
import test.model.DeleteOperation;
import test.model.DeleteOperationInput;
import test.model.DeleteOperationOutput;

It would be nice if side-effects like adding imports would live in a stack with the state and would be picked up only if committed (but I understand that it might be quite hard to implement). Alternatively, if there would be a way to delete certain files before they get written to the file system so that we can can override them in a custom integration.

The reason why I need this is that I replace the content of operation files in the server codegen and because of the requirements I strip RequestContext context from the operation (and also add a couple of other things to the interface but it is not affecting the compilation in my case) and it makes it incompatible with generated service because it expects operations to have 2 arguments (input and context), but in my case I don't need it at all.

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