-
Notifications
You must be signed in to change notification settings - Fork 190
feat: add enum support to response models in ts #1198
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
90a1606
feat: add enum support to response models in ts
ChiragAgg5k cec90d7
proper enum support
ChiragAgg5k 7ee11c9
update react native
ChiragAgg5k 01e3a97
update to use enums in android and kotlin
ChiragAgg5k d413ad8
fix: max length issue
ChiragAgg5k 85db6f4
add swift support
ChiragAgg5k 66adb5a
fix: dart
ChiragAgg5k ee724c1
fix: tests for dart
ChiragAgg5k 8d781b0
fix: import
ChiragAgg5k d007352
fix: enum in apple
ChiragAgg5k 8c5cf9f
fix: android
ChiragAgg5k 62827f4
composer
ChiragAgg5k 8bc6991
remove codeable
ChiragAgg5k 9223f82
enum support for csharp
ChiragAgg5k c21260c
remove deno
ChiragAgg5k 830aeb4
remove import
ChiragAgg5k 09cbde1
fix: swift
ChiragAgg5k 0a1d5e3
fix: format
ChiragAgg5k 5341746
fix: reviews
ChiragAgg5k 783144b
fix: imports
ChiragAgg5k ff36fa1
add mock status
ChiragAgg5k c7344e9
use requestenums name
ChiragAgg5k ca726d5
merge enums
ChiragAgg5k 27e484a
fix: optional handling in swift
ChiragAgg5k f9ad8d8
fix: optional handling in dart
ChiragAgg5k 0417b7b
fix: python, remove deno
ChiragAgg5k 54cd185
fix: node
ChiragAgg5k 80b76ff
Trigger Build
ChiragAgg5k c5884fb
Merge branch 'master' into feat-enum-support
ChiragAgg5k 81e960a
add model support to ruby
ChiragAgg5k 035ee8d
only validate if its not nil
ChiragAgg5k 34e8577
trigger ci
ChiragAgg5k 8b5b334
trigger ci
ChiragAgg5k be203d7
fix: optional issue with enums in android
ChiragAgg5k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,6 @@ jobs: | |
CLINode18, | ||
DartBeta, | ||
DartStable, | ||
Deno1193, | ||
Deno1303, | ||
DotNet60, | ||
DotNet80, | ||
DotNet90, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -469,6 +469,9 @@ public function getFilters(): array | |
} | ||
return $this->toUpperSnakeCase($value); | ||
}), | ||
new TwigFilter('propertyAssignment', function (array $property, array $spec) { | ||
return $this->getPropertyAssignment($property, $spec); | ||
}), | ||
]; | ||
} | ||
|
||
|
@@ -518,6 +521,9 @@ protected function getPropertyType(array $property, array $spec, string $generic | |
if ($property['type'] === 'array') { | ||
$type = 'List<' . $type . '>'; | ||
} | ||
} elseif (isset($property['enum'])) { | ||
$enumName = $property['enumName'] ?? $property['name']; | ||
$type = \ucfirst($enumName); | ||
} else { | ||
$type = $this->getTypeName($property); | ||
} | ||
|
@@ -551,4 +557,71 @@ protected function hasGenericType(?string $model, array $spec): string | |
|
||
return false; | ||
} | ||
|
||
/** | ||
* Generate property assignment logic for model deserialization | ||
* | ||
* @param array $property | ||
* @param array $spec | ||
* @return string | ||
*/ | ||
protected function getPropertyAssignment(array $property, array $spec): string | ||
Comment on lines
+561
to
+568
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like this shift of the complex templating logic into PHP, can we do the same for the other languages changed so far? To keep consistency, and improve readability of their templates too |
||
{ | ||
$propertyName = $property['name']; | ||
$escapedPropertyName = str_replace('$', '\$', $propertyName); | ||
$mapKey = "map[\"$escapedPropertyName\"]"; | ||
|
||
// Handle sub-schema (nested objects) | ||
if (isset($property['sub_schema']) && !empty($property['sub_schema'])) { | ||
$subSchemaClass = $this->toPascalCase($property['sub_schema']); | ||
$hasGenericType = $this->hasGenericType($property['sub_schema'], $spec); | ||
$nestedTypeParam = $hasGenericType ? ', nestedType' : ''; | ||
|
||
if ($property['type'] === 'array') { | ||
return "($mapKey as List<Map<String, Any>>).map { " . | ||
"$subSchemaClass.from(map = it$nestedTypeParam) }"; | ||
} else { | ||
return "$subSchemaClass.from(" . | ||
"map = $mapKey as Map<String, Any>$nestedTypeParam" . | ||
")"; | ||
} | ||
} | ||
|
||
// Handle enum properties | ||
if (isset($property['enum']) && !empty($property['enum'])) { | ||
$enumName = $property['enumName'] ?? $property['name']; | ||
$enumClass = $this->toPascalCase($enumName); | ||
$nullCheck = $property['required'] ? '!!' : ' ?: null'; | ||
|
||
if ($property['required']) { | ||
return "$enumClass.values().find { " . | ||
"it.value == $mapKey as String " . | ||
"}$nullCheck"; | ||
} | ||
|
||
return "$enumClass.values().find { " . | ||
"it.value == ($mapKey as? String) " . | ||
"}$nullCheck"; | ||
} | ||
|
||
// Handle primitive types | ||
$nullableModifier = $property['required'] ? '' : '?'; | ||
|
||
if ($property['type'] === 'integer') { | ||
return "($mapKey as$nullableModifier Number)" . | ||
($nullableModifier ? '?' : '') . '.toLong()'; | ||
} | ||
|
||
if ($property['type'] === 'number') { | ||
return "($mapKey as$nullableModifier Number)" . | ||
($nullableModifier ? '?' : '') . '.toDouble()'; | ||
} | ||
|
||
// Handle other types (string, boolean, etc.) | ||
$kotlinType = $this->getPropertyType($property, $spec); | ||
// Remove nullable modifier from type since we handle it in the cast | ||
$kotlinType = str_replace('?', '', $kotlinType); | ||
|
||
return "$mapKey as$nullableModifier $kotlinType"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.