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: 0 additions & 1 deletion src/main/java/com/chargebee/sdk/java/v4/JavaV4.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.chargebee.sdk.FileOp;
import com.chargebee.sdk.Language;
import com.chargebee.sdk.java.v4.builder.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ private Model createSubModel(String simpleNameSnake, String fullPathSnake, Schem
subModel.setFields(getFields(schema, fullPathSnake, isCompositeArray));
subModel.setEnumFields(getEnumFields(schema, isCompositeArray));
subModel.setSubModels(getSubModels(schema, fullPathSnake));

// Check for custom fields and consent fields support at sub-params level
subModel.setCustomFieldsSupported(SchemaUtil.isCustomFieldsSupported(schema));
subModel.setConsentFieldsSupported(SchemaUtil.isConsentFieldsSupported(schema));

return subModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1620,13 +1620,15 @@ void shouldGenerateCustomFieldMethodWhenSupported() throws IOException {

FileOp.WriteString writeOp = findWriteOp(fileOps, "CustomerListParams.java");
assertThat(writeOp.fileContent)
.contains("public CustomFieldSelector<CustomerListBuilder> customField(String fieldName)");
.contains(
"public CustomFieldSelector<CustomerListBuilder> customField(String fieldName)");
assertThat(writeOp.fileContent)
.contains("return new CustomFieldSelector<>(fieldName, this, queryParams)");
}

@Test
@DisplayName("Should not generate customField method when x-cb-is-custom-fields-supported is false")
@DisplayName(
"Should not generate customField method when x-cb-is-custom-fields-supported is false")
void shouldNotGenerateCustomFieldMethodWhenNotSupported() throws IOException {
Operation getOperation = createGetOperation("customer", "list");
getOperation.getExtensions().put(Extension.IS_CUSTOM_FIELDS_SUPPORTED, false);
Expand Down Expand Up @@ -1674,8 +1676,7 @@ void shouldImportCustomFieldSelectorWhenSupported() throws IOException {
FileOp.WriteString writeOp = findWriteOp(fileOps, "SubscriptionListParams.java");
assertThat(writeOp.fileContent)
.contains("import com.chargebee.v4.filters.CustomFieldSelector;");
assertThat(writeOp.fileContent)
.contains("import com.chargebee.v4.filters.BooleanFilter;");
assertThat(writeOp.fileContent).contains("import com.chargebee.v4.filters.BooleanFilter;");
}

@Test
Expand All @@ -1697,11 +1698,13 @@ void shouldGenerateCorrectBuilderTypeForDifferentOperations() throws IOException

FileOp.WriteString customerWriteOp = findWriteOp(fileOps, "CustomerListParams.java");
assertThat(customerWriteOp.fileContent)
.contains("public CustomFieldSelector<CustomerListBuilder> customField(String fieldName)");
.contains(
"public CustomFieldSelector<CustomerListBuilder> customField(String fieldName)");

FileOp.WriteString subscriptionWriteOp = findWriteOp(fileOps, "SubscriptionListParams.java");
assertThat(subscriptionWriteOp.fileContent)
.contains("public CustomFieldSelector<SubscriptionListBuilder> customField(String fieldName)");
.contains(
"public CustomFieldSelector<SubscriptionListBuilder> customField(String fieldName)");
}

@Test
Expand All @@ -1719,7 +1722,8 @@ void shouldGenerateCustomFieldMethodWithJavadoc() throws IOException {
FileOp.WriteString writeOp = findWriteOp(fileOps, "CustomerListParams.java");
assertThat(writeOp.fileContent).contains("Create a filter for a custom field");
assertThat(writeOp.fileContent).contains("@param fieldName the custom field name");
assertThat(writeOp.fileContent).contains("@return CustomFieldSelector for choosing filter type");
assertThat(writeOp.fileContent)
.contains("@return CustomFieldSelector for choosing filter type");
}

@Test
Expand All @@ -1742,7 +1746,8 @@ void shouldHandleCustomFieldSupportWithOtherFeatures() throws IOException {
FileOp.WriteString writeOp = findWriteOp(fileOps, "CustomerListParams.java");
// Verify customField method is generated
assertThat(writeOp.fileContent)
.contains("public CustomFieldSelector<CustomerListBuilder> customField(String fieldName)");
.contains(
"public CustomFieldSelector<CustomerListBuilder> customField(String fieldName)");
// Verify other features are still generated
assertThat(writeOp.fileContent).contains("public FirstNameFilter firstName()");
assertThat(writeOp.fileContent).contains("public CreatedAtFilter createdAt()");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,104 @@ void shouldHandleCustomFieldsExtensionSetToFalse() throws IOException {
FileOp.WriteString writeOp = findWriteOp(fileOps, "TestCreateParams.java");
assertThat(writeOp.fileContent).doesNotContain("customField");
}

@Test
@DisplayName(
"Should add custom field methods to sub-params when x-cb-is-custom-fields-supported is true"
+ " at sub-params level")
void shouldAddCustomFieldMethodsToSubParamsWithCustomFieldsExtension() throws IOException {
// Create a nested object schema (billing_address) with custom fields support
ObjectSchema billingAddressSchema = new ObjectSchema();
billingAddressSchema.addProperty("line1", new StringSchema());
billingAddressSchema.addProperty("city", new StringSchema());
billingAddressSchema.addExtension("x-cb-is-custom-fields-supported", true);

// Parent request schema without custom fields support
ObjectSchema requestSchema = new ObjectSchema();
requestSchema.addProperty("first_name", new StringSchema());
requestSchema.addProperty("billing_address", billingAddressSchema);

MediaType mediaType = new MediaType();
mediaType.setSchema(requestSchema);

Content content = new Content();
content.addMediaType("application/x-www-form-urlencoded", mediaType);

RequestBody requestBody = new RequestBody();
requestBody.setContent(content);

Operation postOp = new Operation();
postOp.setOperationId("create_customer");
postOp.addExtension(Extension.SDK_METHOD_NAME, "create");
postOp.addExtension(Extension.RESOURCE_ID, "customer");
postOp.setRequestBody(requestBody);

PathItem pathItem = new PathItem();
pathItem.setPost(postOp);

Paths paths = new Paths();
paths.addPathItem("/customers", pathItem);
openAPI.setPaths(paths);

paramsBuilder.withOutputDirectoryPath(outputPath).withTemplate(mockTemplate);

List<FileOp> fileOps = paramsBuilder.build(openAPI);

FileOp.WriteString writeOp = findWriteOp(fileOps, "CustomerCreateParams.java");
// Parent should NOT have custom fields
assertThat(writeOp.fileContent).doesNotContain("public CustomerCreateBuilder customField(");
// Sub-params (BillingAddress) should have custom fields
assertThat(writeOp.fileContent).contains("public BillingAddressBuilder customField(");
assertThat(writeOp.fileContent).contains("public BillingAddressBuilder customFields(");
}

@Test
@DisplayName(
"Should not add custom field methods to sub-params without x-cb-is-custom-fields-supported")
void shouldNotAddCustomFieldMethodsToSubParamsWithoutExtension() throws IOException {
// Create a nested object schema (billing_address) without custom fields support
ObjectSchema billingAddressSchema = new ObjectSchema();
billingAddressSchema.addProperty("line1", new StringSchema());
billingAddressSchema.addProperty("city", new StringSchema());

// Parent request schema with custom fields support
ObjectSchema requestSchema = new ObjectSchema();
requestSchema.addProperty("first_name", new StringSchema());
requestSchema.addProperty("billing_address", billingAddressSchema);
requestSchema.addExtension("x-cb-is-custom-fields-supported", true);

MediaType mediaType = new MediaType();
mediaType.setSchema(requestSchema);

Content content = new Content();
content.addMediaType("application/x-www-form-urlencoded", mediaType);

RequestBody requestBody = new RequestBody();
requestBody.setContent(content);

Operation postOp = new Operation();
postOp.setOperationId("create_customer");
postOp.addExtension(Extension.SDK_METHOD_NAME, "create");
postOp.addExtension(Extension.RESOURCE_ID, "customer");
postOp.setRequestBody(requestBody);

PathItem pathItem = new PathItem();
pathItem.setPost(postOp);

Paths paths = new Paths();
paths.addPathItem("/customers", pathItem);
openAPI.setPaths(paths);

paramsBuilder.withOutputDirectoryPath(outputPath).withTemplate(mockTemplate);

List<FileOp> fileOps = paramsBuilder.build(openAPI);

FileOp.WriteString writeOp = findWriteOp(fileOps, "CustomerCreateParams.java");
// Parent should have custom fields
assertThat(writeOp.fileContent).contains("public CustomerCreateBuilder customField(");
// Sub-params (BillingAddress) should NOT have custom fields
assertThat(writeOp.fileContent).doesNotContain("public BillingAddressBuilder customField(");
}
}

private FileOp.WriteString findWriteOp(List<FileOp> fileOps, String fileName) {
Expand Down