Skip to content

Commit

Permalink
Add optional builder pattern to OpenAPI mustache POJO template (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
newtork authored Jul 30, 2024
1 parent e370185 commit e162af6
Show file tree
Hide file tree
Showing 37 changed files with 1,966 additions and 37 deletions.
5 changes: 5 additions & 0 deletions datamodel/openapi/openapi-api-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
<deleteOutputDirectory>true</deleteOutputDirectory>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<sapCopyrightHeader>true</sapCopyrightHeader>
<additionalProperties>
<pojoBuilderMethodName>create</pojoBuilderMethodName>
<pojoBuildMethodName />
<pojoConstructorVisibility>protected</pojoConstructorVisibility>
</additionalProperties>
</configuration>
</plugin>
<!-- Enable formatter to always run on the generated code -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.google.common.annotations.Beta;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.datamodel.openapi.sample.model.Order; //NOPMD
import com.sap.cloud.sdk.datamodel.openapi.sample.model.Order;
import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient;
import com.sap.cloud.sdk.services.openapi.core.AbstractOpenApiService;
import com.sap.cloud.sdk.services.openapi.core.OpenApiRequestException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import com.google.common.annotations.Beta;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.datamodel.openapi.sample.model.Soda; //NOPMD
import com.sap.cloud.sdk.datamodel.openapi.sample.model.SodaWithId; //NOPMD
import com.sap.cloud.sdk.datamodel.openapi.sample.model.Soda;
import com.sap.cloud.sdk.datamodel.openapi.sample.model.SodaWithId;
import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient;
import com.sap.cloud.sdk.services.openapi.core.AbstractOpenApiService;
import com.sap.cloud.sdk.services.openapi.core.OpenApiRequestException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class Order
@JsonAnyGetter
private final Map<String, Object> cloudSdkCustomFields = new LinkedHashMap<>();

protected Order()
{
}

/**
* Set the productId of this {@link Order} instance and return the same instance.
*
Expand Down Expand Up @@ -335,4 +339,43 @@ private String toIndentedString( final java.lang.Object o )
return o.toString().replace("\n", "\n ");
}

/**
* Create a type-safe, fluent-api builder object to construct a new {@link Order} instance with all required
* arguments.
*/
public static Builder create()
{
return ( productId ) -> ( quantity ) -> new Order().productId(productId).quantity(quantity);
}

/**
* Builder helper class.
*/
public interface Builder
{
/**
* Set the productId of this {@link Order} instance.
*
* @param productId
* The productId of this {@link Order}
* @return The Order builder.
*/
Builder1 productId( @Nonnull final Long productId );
}

/**
* Builder helper class.
*/
public interface Builder1
{
/**
* Set the quantity of this {@link Order} instance.
*
* @param quantity
* The quantity of this {@link Order}
* @return The Order instance.
*/
Order quantity( @Nonnull final Integer quantity );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class OrderWithTimestamp
private Float totalPrice;

@JsonProperty( "typelessProperty" )
private Object typelessProperty = null;
private Object typelessProperty;

@JsonProperty( "nullableProperty" )
private String nullableProperty;
Expand All @@ -61,6 +61,10 @@ public class OrderWithTimestamp
@JsonAnyGetter
private final Map<String, Object> cloudSdkCustomFields = new LinkedHashMap<>();

protected OrderWithTimestamp()
{
}

/**
* Set the productId of this {@link OrderWithTimestamp} instance and return the same instance.
*
Expand Down Expand Up @@ -378,4 +382,43 @@ private String toIndentedString( final java.lang.Object o )
return o.toString().replace("\n", "\n ");
}

/**
* Create a type-safe, fluent-api builder object to construct a new {@link OrderWithTimestamp} instance with all
* required arguments.
*/
public static Builder create()
{
return ( productId ) -> ( quantity ) -> new OrderWithTimestamp().productId(productId).quantity(quantity);
}

/**
* Builder helper class.
*/
public interface Builder
{
/**
* Set the productId of this {@link OrderWithTimestamp} instance.
*
* @param productId
* The productId of this {@link OrderWithTimestamp}
* @return The OrderWithTimestamp builder.
*/
Builder1 productId( @Nonnull final Long productId );
}

/**
* Builder helper class.
*/
public interface Builder1
{
/**
* Set the quantity of this {@link OrderWithTimestamp} instance.
*
* @param quantity
* The quantity of this {@link OrderWithTimestamp}
* @return The OrderWithTimestamp instance.
*/
OrderWithTimestamp quantity( @Nonnull final Integer quantity );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class Soda
@JsonAnyGetter
private final Map<String, Object> cloudSdkCustomFields = new LinkedHashMap<>();

protected Soda()
{
}

/**
* Set the name of this {@link Soda} instance and return the same instance.
*
Expand Down Expand Up @@ -294,4 +298,74 @@ private String toIndentedString( final java.lang.Object o )
return o.toString().replace("\n", "\n ");
}

/**
* Create a type-safe, fluent-api builder object to construct a new {@link Soda} instance with all required
* arguments.
*/
public static Builder create()
{
return ( name ) -> (
brand ) -> ( quantity ) -> ( price ) -> new Soda().name(name).brand(brand).quantity(quantity).price(price);
}

/**
* Builder helper class.
*/
public interface Builder
{
/**
* Set the name of this {@link Soda} instance.
*
* @param name
* The name of this {@link Soda}
* @return The Soda builder.
*/
Builder1 name( @Nonnull final String name );
}

/**
* Builder helper class.
*/
public interface Builder1
{
/**
* Set the brand of this {@link Soda} instance.
*
* @param brand
* The brand of this {@link Soda}
* @return The Soda builder.
*/
Builder2 brand( @Nonnull final String brand );
}

/**
* Builder helper class.
*/
public interface Builder2
{
/**
* Set the quantity of this {@link Soda} instance.
*
* @param quantity
* The quantity of this {@link Soda}
* @return The Soda builder.
*/
Builder3 quantity( @Nonnull final Integer quantity );
}

/**
* Builder helper class.
*/
public interface Builder3
{
/**
* Set the price of this {@link Soda} instance.
*
* @param price
* The price of this {@link Soda}
* @return The Soda instance.
*/
Soda price( @Nonnull final Float price );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class SodaWithId
@JsonAnyGetter
private final Map<String, Object> cloudSdkCustomFields = new LinkedHashMap<>();

protected SodaWithId()
{
}

/**
* Set the name of this {@link SodaWithId} instance and return the same instance.
*
Expand Down Expand Up @@ -335,4 +339,74 @@ private String toIndentedString( final java.lang.Object o )
return o.toString().replace("\n", "\n ");
}

/**
* Create a type-safe, fluent-api builder object to construct a new {@link SodaWithId} instance with all required
* arguments.
*/
public static Builder create()
{
return ( name ) -> ( brand ) -> (
quantity ) -> ( price ) -> new SodaWithId().name(name).brand(brand).quantity(quantity).price(price);
}

/**
* Builder helper class.
*/
public interface Builder
{
/**
* Set the name of this {@link SodaWithId} instance.
*
* @param name
* The name of this {@link SodaWithId}
* @return The SodaWithId builder.
*/
Builder1 name( @Nonnull final String name );
}

/**
* Builder helper class.
*/
public interface Builder1
{
/**
* Set the brand of this {@link SodaWithId} instance.
*
* @param brand
* The brand of this {@link SodaWithId}
* @return The SodaWithId builder.
*/
Builder2 brand( @Nonnull final String brand );
}

/**
* Builder helper class.
*/
public interface Builder2
{
/**
* Set the quantity of this {@link SodaWithId} instance.
*
* @param quantity
* The quantity of this {@link SodaWithId}
* @return The SodaWithId builder.
*/
Builder3 quantity( @Nonnull final Integer quantity );
}

/**
* Builder helper class.
*/
public interface Builder3
{
/**
* Set the price of this {@link SodaWithId} instance.
*
* @param price
* The price of this {@link SodaWithId}
* @return The SodaWithId instance.
*/
SodaWithId price( @Nonnull final Float price );
}

}
Loading

0 comments on commit e162af6

Please sign in to comment.