Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/configs/java-micronaut-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ additionalProperties:
build: "all"
test: "spock"
requiredPropertiesInConstructor: "false"
visitable: "true"
1 change: 1 addition & 0 deletions docs/generators/java-micronaut-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|title|Client service name| |null|
|useBeanValidation|Use BeanValidation API annotations| |true|
|useOptional|Use Optional container for optional parameters| |false|
|visitable|Generate visitor for subtypes with a discriminator| |false|
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|

## SUPPORTED VENDOR EXTENSIONS
Expand Down
1 change: 1 addition & 0 deletions docs/generators/java-micronaut-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|useAuth|Whether to import authorization and to annotate controller methods accordingly| |true|
|useBeanValidation|Use BeanValidation API annotations| |true|
|useOptional|Use Optional container for optional parameters| |false|
|visitable|Generate visitor for subtypes with a discriminator| |false|
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|

## SUPPORTED VENDOR EXTENSIONS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public abstract class JavaMicronautAbstractCodegen extends AbstractJavaCodegen i
public static final String OPT_REQUIRED_PROPERTIES_IN_CONSTRUCTOR = "requiredPropertiesInConstructor";
public static final String OPT_MICRONAUT_VERSION = "micronautVersion";
public static final String OPT_USE_AUTH = "useAuth";
public static final String OPT_VISITABLE = "visitable";
public static final String OPT_DATE_LIBRARY_JAVA8 = "java8";
public static final String OPT_DATE_LIBRARY_JAVA8_LOCAL_DATETIME = "java8-localdatetime";
public static final String OPT_DATE_FORMAT = "dateFormat";
Expand All @@ -36,6 +37,7 @@ public abstract class JavaMicronautAbstractCodegen extends AbstractJavaCodegen i
protected String title;
protected boolean useBeanValidation;
protected boolean useOptional;
protected boolean visitable;
protected String buildTool;
protected String testTool;
protected boolean requiredPropertiesInConstructor = true;
Expand All @@ -55,6 +57,7 @@ public JavaMicronautAbstractCodegen() {
// Set all the fields
useBeanValidation = true;
useOptional = false;
visitable = false;
buildTool = OPT_BUILD_ALL;
testTool = OPT_TEST_JUNIT;
outputFolder = "generated-code/java-micronaut-client";
Expand Down Expand Up @@ -99,6 +102,7 @@ public JavaMicronautAbstractCodegen() {
cliOptions.add(new CliOption(OPT_MICRONAUT_VERSION, "Micronaut version, only >=3.0.0 versions are supported").defaultValue(micronautVersion));
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations", useBeanValidation));
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL, "Use Optional container for optional parameters", useOptional));
cliOptions.add(CliOption.newBoolean(OPT_VISITABLE, "Generate visitor for subtypes with a discriminator", visitable));
cliOptions.add(CliOption.newBoolean(OPT_REQUIRED_PROPERTIES_IN_CONSTRUCTOR, "Allow only to create models with all the required properties provided in constructor", requiredPropertiesInConstructor));

CliOption buildToolOption = new CliOption(OPT_BUILD, "Specify for which build tool to generate files").defaultValue(buildTool);
Expand Down Expand Up @@ -167,6 +171,11 @@ public void processOpts() {
}
writePropertyBack(USE_OPTIONAL, useOptional);

if (additionalProperties.containsKey(OPT_VISITABLE)) {
this.setVisitable(convertPropertyToBoolean(OPT_VISITABLE));
}
writePropertyBack(OPT_VISITABLE, visitable);

if (additionalProperties.containsKey(OPT_REQUIRED_PROPERTIES_IN_CONSTRUCTOR)) {
this.requiredPropertiesInConstructor = convertPropertyToBoolean(OPT_REQUIRED_PROPERTIES_IN_CONSTRUCTOR);
}
Expand Down Expand Up @@ -320,6 +329,10 @@ public void setUseOptional(boolean useOptional) {
this.useOptional = useOptional;
}

public void setVisitable(boolean visitable) {
this.visitable = visitable;
}

@Override
public String toApiVarName(String name) {
String apiVarName = super.toApiVarName(name);
Expand All @@ -337,6 +350,10 @@ public boolean isUseOptional() {
return useOptional;
}

public boolean isVisitable() {
return visitable;
}

@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
objs = super.postProcessOperationsWithModels(objs, allModels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,36 @@ Declare the class with extends and implements
}
};
{{/parcelableModel}}
{{#visitable}}
{{#discriminator}}
/**
* Accept the visitor and invoke it for the specific {{classname}} type.
*
* @param visitor the {{classname}} visitor
* @param <T> the return type of the visitor
* @return the result from the visitor
*/
public abstract <T> T accept(Visitor<T> visitor);

/**
* A {{classname}} visitor implementation allows visiting the various {{classname}} types.
*
* @param <R> the return type of the visitor
*/
public interface Visitor<R> {
{{#discriminator.mappedModels}}
R visit{{modelName}}({{modelName}} value);
{{/discriminator.mappedModels}}
}

{{/discriminator}}
{{#parent}}
{{#interfaces.0}}
@Override
public <T> T accept({{{parent}}}.Visitor<T> visitor) {
return visitor.visit{{classname}}(this);
}
{{/interfaces.0}}
{{/parent}}
{{/visitable}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,24 @@ private String toIndentedString(Object o) {
return o.toString().replace("\n", "\n ");
}

/**
* Accept the visitor and invoke it for the specific Animal type.
*
* @param visitor the Animal visitor
* @param <T> the return type of the visitor
* @return the result from the visitor
*/
public abstract <T> T accept(Visitor<T> visitor);

/**
* A Animal visitor implementation allows visiting the various Animal types.
*
* @param <R> the return type of the visitor
*/
public interface Visitor<R> {
R visitBigCat(BigCat value);
R visitCat(Cat value);
R visitDog(Dog value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,8 @@ private String toIndentedString(Object o) {
return o.toString().replace("\n", "\n ");
}

@Override
public <T> T accept(Cat.Visitor<T> visitor) {
return visitor.visitBigCat(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@ private String toIndentedString(Object o) {
return o.toString().replace("\n", "\n ");
}

@Override
public <T> T accept(Animal.Visitor<T> visitor) {
return visitor.visitCat(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@ private String toIndentedString(Object o) {
return o.toString().replace("\n", "\n ");
}

@Override
public <T> T accept(Animal.Visitor<T> visitor) {
return visitor.visitDog(this);
}
}