fix: quote non-identifier dart_mappable enum values for nullable/list types#468
Open
RootSoft wants to merge 1 commit into
Open
fix: quote non-identifier dart_mappable enum values for nullable/list types#468RootSoft wants to merge 1 commit into
RootSoft wants to merge 1 commit into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR fixes enum generation for dart_mappable when an enum's schema uses a JSON Schema type list such as
["string", "null"](common in OpenAPI 3.1 specs, e.g. those generated from Laravel form request rules).Problem
Generated dart_mappable enums were emitting the JSON value as a bare identifier instead of a string literal:
@MappableValue(...)requires a constant expression, so an unquoted identifier likehidden_gemorprice_ascis not valid Dart. The generated file fails to compile with:This only happened for enum values that are not valid Dart identifiers (contain underscores, dashes, digits, etc.) when the enum type was declared as a nullable/list type.
Root cause
The dart_mappable enum template only wrapped values in quotes when the enum's parsed type was exactly
'string':When a schema declares:
the parser stores the enum type as the literal string
"[string, null]"— not"string". (This is intentional: the nullability is preserved in the type string sotoDartType()can deriveString?for the JSON field.) Because"[string, null]" != "string", the condition was false and the value was emitted unquoted.The freezed / json_serializable template already handled this correctly by falling back to quoting any non-numeric value. The dart_mappable template was missing that fallback.
Example
This schema:
previously generated (invalid):
Fix
The value-literal logic is now shared between the freezed and dart_mappable templates via a single helper. It quotes strings and any non-numeric value, leaves numeric values bare, and keeps
nullasnull:Behavior after the fix:
@MappableValue('price_asc'),@MappableValue('hidden_gem')@MappableValue(0),@MappableValue(-1)@MappableValue(null)This restores valid, compilable dart_mappable output for enums declared with nullable/list types.
Tests
Added a new e2e test
enum_types_list_mappable(a dart_mappable variant of the existingenum_types_listfixture) that covers["string", "null"],["string", "integer"], and enum properties nested inside objects. The full e2e suite and generator/parser unit tests pass.