Skip to content

Conversation

sbansla
Copy link
Contributor

@sbansla sbansla commented Jul 24, 2025

Fixes

Added a centralized serialization so that we don't have to identify variable type in twilio-oai-generator.

Checklist

  • I acknowledge that all my contributions will be made under the project's license
  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the Contribution Guidelines and my PR follows them
  • I have titled the PR appropriately
  • I have updated my branch with the main branch
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation about the functionality in the appropriate .md file
  • I have added inline documentation to the code I modified

If you have questions, please file a support ticket, or create a GitHub Issue in this repository.

@sbansla sbansla marked this pull request as draft July 24, 2025 12:41
@sbansla sbansla marked this pull request as ready for review August 8, 2025 05:13
Copy link

sonarqubecloud bot commented Aug 8, 2025

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a centralized serialization utility to standardize how different data types are converted to strings and added to HTTP requests. The change eliminates the need for type-specific handling in the twilio-oai-generator by providing a common Serializer class.

  • Added a Serializer class with overloaded toString methods for various data types
  • Introduced a ParameterType enum to distinguish between query, header, and URL-encoded parameters
  • Comprehensive test coverage for all supported data types and edge cases

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/main/java/com/twilio/converter/Serializer.java Core serializer implementation with generic and date-specific toString methods
src/test/java/com/twilio/converter/SerializerTest.java Comprehensive test suite covering all data types and parameter types
src/main/java/com/twilio/constant/EnumConstants.java Added ParameterType enum for distinguishing parameter destinations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


private static <T> String convertToString(T value) {
if (value instanceof Map) {
return Converter.mapToJson((Map<String, ? extends Object>) value);
Copy link
Preview

Copilot AI Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast to (Map<String, ? extends Object>) is unsafe and may cause ClassCastException at runtime. Consider using a type-safe approach with instanceof checks or generic bounds to ensure the cast is valid.

Suggested change
return Converter.mapToJson((Map<String, ? extends Object>) value);
Map<?, ?> map = (Map<?, ?>) value;
boolean allStringKeys = true;
for (Object key : map.keySet()) {
if (!(key instanceof String)) {
allStringKeys = false;
break;
}
}
if (allStringKeys) {
@SuppressWarnings("unchecked")
Map<String, ? extends Object> stringKeyMap = (Map<String, ? extends Object>) map;
return Converter.mapToJson(stringKeyMap);
} else {
// Fallback: not a Map<String, ?>, use toString
return String.valueOf(value);
}

Copilot uses AI. Check for mistakes.

@Test
public void testToStringWithLocalDate() {
Request request = buildRequest();
LocalDate date = LocalDate.of(2025, 07, 1);
Copy link
Preview

Copilot AI Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The month value 07 should be written as 7 instead of using octal notation. While 07 equals 7 in this context, using leading zeros can be misleading and suggests octal representation.

Copilot uses AI. Check for mistakes.

Comment on lines 197 to 198
LocalDate dateBefore = LocalDate.of(2025, 07, 5);
LocalDate dateAfter = LocalDate.of(2025, 07, 1);
Copy link
Preview

Copilot AI Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The month value 07 should be written as 7 instead of using octal notation. While 07 equals 7 in this context, using leading zeros can be misleading and suggests octal representation.

Copilot uses AI. Check for mistakes.

Request request = buildRequest();
LocalDate date = null;
LocalDate dateBefore = null;
LocalDate dateAfter = LocalDate.of(2025, 07, 1);
Copy link
Preview

Copilot AI Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The month value 07 should be written as 7 instead of using octal notation. While 07 equals 7 in this context, using leading zeros can be misleading and suggests octal representation.

Copilot uses AI. Check for mistakes.

public void testToStringWithLocalDateRangeAfterNull() {
Request request = buildRequest();
LocalDate date = null;
LocalDate dateBefore = LocalDate.of(2025, 07, 5);
Copy link
Preview

Copilot AI Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The month value 07 should be written as 7 instead of using octal notation. While 07 equals 7 in this context, using leading zeros can be misleading and suggests octal representation.

Copilot uses AI. Check for mistakes.

addParamToRequest(request, key, stringValue, parameterType);
}

private static <T> String convertToString(T value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The objectToJson() can be used here instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean using mapper to convert to string ?
We can not use it, because \n (new line) is treated as actual new line for toString method, whereas mapper keep /n as it as.

break;
case URLENCODED:
request.addPostParam(key, value);
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will body params be added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed json content type serialization.

@tiwarishubham635 tiwarishubham635 force-pushed the main branch 2 times, most recently from 6533f63 to 78a8ccd Compare September 9, 2025 08:08
Copy link

sonarqubecloud bot commented Sep 9, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants