Skip to content
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

Custom mapping support #12

Open
apenlor opened this issue Oct 31, 2023 · 0 comments
Open

Custom mapping support #12

apenlor opened this issue Oct 31, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@apenlor
Copy link
Contributor

apenlor commented Oct 31, 2023

As a developer, I want to fully customize how a data type is processed.

For this to be available, we could extend builder object methods to overwrite the DSL definition generated.

For example: Having a Student object that contains an Example object, besides having available the setExample we can add something like `setExampleDslPart'

        //already available
        StudentBuilder studentBuilder = new StudentBuilder();
        studentBuilder.setExample(new Example( ... ))
        DslPart student = studentBuilder.build();
        import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonBody;

         DslPart dslPartForExample = newJsonBody(object -> {
            object.stringType("id", "1");
            object.stringType("name", "Fake name");
            object.stringType("email", "[email protected]");
            object.numberType("studentNumber", 23);
        }).build();

        StudentBuilder studentBuilder = new StudentBuilder();
        //to be added
        studentBuilder.setExampleDslPart(dslPartForExample)
        DslPart student = studentBuilder.build();

Something like this would open the possibility of fully customizing how an object is managed by the library.

Also would be good to have a way to configure this behavior at the annotation level, to avoid having to repeat this assignation in each one of the '@Pact' definitions. Maybe use a new annotation that references an implementation class.

For example:

@PactDslBodyBuilder
public class Student {

  @CustomMapping(CustomMapping.class)
  private Example example;

}

That referenced class should contain an annotated method that references Example.class, to indicate that is the DslPart generator method:

public class CustomMapping {

  @CustomMappingGenerator(Example.class)
  public Example getExampleDslPart (){
         return newJsonBody(object -> {
            object.stringType("id", "1");
            object.stringType("name", "Fake name");
            object.stringType("email", "[email protected]");
            object.numberType("studentNumber", 23);
        }).build();
 }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

1 participant