@@ -34,7 +34,7 @@ class ClientGenerator extends BaseGenerator {
3434 // ------------------------------------------
3535
3636 @override
37- Future < void > generate () async {
37+ void generate () {
3838 final clientName = '${package .pascalCase }Client' ;
3939 final clientException = '${clientName }Exception' ;
4040
@@ -43,28 +43,25 @@ class ClientGenerator extends BaseGenerator {
4343 for (final e
4444 in (spec.components? .securitySchemes ?? < String , SecurityScheme > {})
4545 .entries) {
46- e.value. mapOrNull (
47- apiKey : (a) {
48- switch (a. location) {
46+ switch ( e.value) {
47+ case SecuritySchemeApiKey (location : final location) :
48+ switch (location) {
4949 case ApiKeyLocation .cookie:
5050 security[AuthType .keyCookie] = e.value;
5151 case ApiKeyLocation .header:
5252 security[AuthType .keyHeader] = e.value;
5353 case ApiKeyLocation .query:
5454 security[AuthType .keyQuery] = e.value;
5555 }
56- },
57- http: (o) {
58- if (o.scheme == HttpSecurityScheme .basic) {
56+ case SecuritySchemeHttp (scheme: final scheme):
57+ if (scheme == HttpSecurityScheme .basic) {
5958 security[AuthType .httpBasic] = e.value;
60- } else if (o. scheme == HttpSecurityScheme .bearer) {
59+ } else if (scheme == HttpSecurityScheme .bearer) {
6160 security[AuthType .httpBearer] = e.value;
6261 }
63- },
64- openIdConnect: (o) {
62+ case SecuritySchemeOpenIdConnect ():
6563 security[AuthType .openId] = e.value;
66- },
67- );
64+ }
6865 }
6966
7067 // Check if there is a global security scheme to apply to all endpoints
@@ -88,8 +85,8 @@ class ClientGenerator extends BaseGenerator {
8885 authVariables.add ('final String $passwordVar ;' );
8986 }
9087 if (security.keys.contains (AuthType .httpBearer)) {
91- await security[AuthType .httpBearer]? . mapOrNull (
92- http : (o) async {
88+ switch ( security[AuthType .httpBearer]) {
89+ case SecuritySchemeHttp () :
9390 authInputs.add ("this.$bearerTokenVar = ''" );
9491 authVariables.add ('String $bearerTokenVar ;' );
9592 authRequestHeader = """
@@ -98,12 +95,11 @@ class ClientGenerator extends BaseGenerator {
9895 headers['${HttpHeaders .authorizationHeader }'] = 'Bearer \$ $bearerTokenVar ';
9996 }
10097 """ ;
101- },
102- );
98+ }
10399 }
104100 if (security.keys.contains (AuthType .openId)) {
105- await security[AuthType .openId]? . mapOrNull (
106- openIdConnect : (o) async {
101+ switch ( security[AuthType .openId]) {
102+ case SecuritySchemeOpenIdConnect () :
107103 authInputs.add ("this.$accessTokenVar = ''" );
108104 authVariables.add ('String $accessTokenVar ;' );
109105 authRequestHeader = """
@@ -112,8 +108,7 @@ class ClientGenerator extends BaseGenerator {
112108 headers['${HttpHeaders .authorizationHeader }'] = 'Bearer \$ $accessTokenVar ';
113109 }
114110 """ ;
115- },
116- );
111+ }
117112 }
118113 }
119114 // Generate auth input code
@@ -580,29 +575,26 @@ class $clientName {
580575 for (final s in security) {
581576 if (schemes.containsKey (s.name)) {
582577 final scheme = schemes[s.name];
583- scheme ? . mapOrNull (
584- apiKey : (a) {
585- switch (a. location) {
578+ switch (scheme) {
579+ case SecuritySchemeApiKey (location : final location) :
580+ switch (location) {
586581 case ApiKeyLocation .query:
587582 auth[AuthType .keyQuery] = scheme;
588583 case ApiKeyLocation .header:
589584 auth[AuthType .keyHeader] = scheme;
590585 case ApiKeyLocation .cookie:
591586 auth[AuthType .keyCookie] = scheme;
592587 }
593- },
594- http: (a) {
595- switch (a.scheme) {
588+ case SecuritySchemeHttp (scheme: final securityScheme):
589+ switch (securityScheme) {
596590 case HttpSecurityScheme .basic:
597591 auth[AuthType .httpBasic] = scheme;
598592 case HttpSecurityScheme .bearer:
599593 auth[AuthType .httpBearer] = scheme;
600594 }
601- },
602- openIdConnect: (a) {
595+ case SecuritySchemeOpenIdConnect ():
603596 auth[AuthType .openId] = scheme;
604- },
605- );
597+ }
606598 }
607599 }
608600 return auth;
@@ -658,9 +650,10 @@ class $clientName {
658650
659651 for (final a in auth.keys) {
660652 final s = auth[a];
661- final name = s? .mapOrNull (
662- apiKey: (value) => value.name,
663- );
653+ final name = switch (s) {
654+ SecuritySchemeApiKey (name: final name) => name,
655+ _ => null ,
656+ };
664657
665658 if (a == AuthType .keyQuery) {
666659 queryParams.add ("if ($apiKeyVar .isNotEmpty) '$name ': $apiKeyVar " );
@@ -743,66 +736,69 @@ class $clientName {
743736 if (pName.isEmpty) {
744737 throw Exception ('Parameter name or reference is required: $param ' );
745738 }
746- param. map (
747- cookie : (p) {
739+ switch (param) {
740+ case ParameterCookie () :
748741 // Do nothing
749- },
750- header: (p) {
751- String hCode = "'${p .name }': $pNameCamel " ;
752- String pType = p.schema.toDartType ();
753- if (p.required == true ) {
742+ break ;
743+ case ParameterHeader (
744+ name: final name,
745+ schema: final schema,
746+ required : final required ,
747+ description: final description
748+ ):
749+ String hCode = "'$name ': $pNameCamel " ;
750+ String pType = schema.toDartType ();
751+ if (required == true ) {
754752 pType = 'required $pType ' ;
755753 } else {
756754 pType = '$pType ?' ;
757755 hCode = 'if ($pNameCamel != null) $hCode ' ;
758756 }
759757 input.add ('$pType $pNameCamel ' );
760758 inputDescription.add (
761- "`$pNameCamel `: ${p . description ?? 'No description' }" ,
759+ "`$pNameCamel `: ${description ?? 'No description' }" ,
762760 );
763761 headerParams.add (hCode);
764- },
765- query: (p) {
766- String pType = p.schema.toDartType ();
767- Object ? pDefaultValue = p.schema.defaultValue;
762+ case ParameterQuery (
763+ name: final name,
764+ schema: final schema,
765+ required : final required ,
766+ description: final description
767+ ):
768+ String pType = schema.toDartType ();
769+ Object ? pDefaultValue = schema.defaultValue;
768770 // Handle nullable types
769771 if (pDefaultValue == null &&
770- p. required != true &&
772+ required != true &&
771773 ! pType.contains ('?' )) {
772774 pType = '$pType ?' ;
773775 }
774- String qCode = p.schema.maybeMap (
775- enumeration: (o) {
776- // Convert enum to string for query parameter code
777- if (pType == 'String' ) {
778- return "'${p .name }': $pNameCamel " ;
779- } else {
780- return "'${p .name }': $pNameCamel .name" ;
781- }
782- },
783- orElse: () {
784- return "'${p .name }': $pNameCamel " ;
785- },
786- );
776+ String qCode = switch (schema) {
777+ // Convert enum to string for query parameter code
778+ SchemaEnum () => pType == 'String'
779+ ? "'$name ': $pNameCamel "
780+ : "'$name ': $pNameCamel .name" ,
781+ _ => "'$name ': $pNameCamel " ,
782+ };
787783
788784 // Handle enumeration default values
789- p.schema. mapOrNull (
790- string : (value) {
785+ switch (schema) {
786+ case SchemaString () :
791787 if (pDefaultValue != null ) {
792788 pDefaultValue = "'$pDefaultValue '" ;
793789 }
794- },
795- enumeration: (value) {
790+ case SchemaEnum ():
796791 if (pDefaultValue != null ) {
797- if (p. schema.ref != null && pType != 'String' ) {
798- pDefaultValue = '${p . schema .ref }.$pDefaultValue ' ;
792+ if (schema.ref != null && pType != 'String' ) {
793+ pDefaultValue = '${schema .ref }.$pDefaultValue ' ;
799794 } else {
800795 pDefaultValue = "'$pDefaultValue '" ;
801796 }
802797 }
803- },
804- );
805- if (p.required == true ) {
798+ default :
799+ // Do nothing
800+ }
801+ if (required == true ) {
806802 pType = 'required $pType ' ;
807803 } else {
808804 if (pType.contains ('?' )) {
@@ -815,19 +811,17 @@ class $clientName {
815811 input.add ('$pType $pNameCamel ' );
816812 }
817813 inputDescription.add (
818- "`$pNameCamel `: ${p . description ?? 'No description' }" ,
814+ "`$pNameCamel `: ${description ?? 'No description' }" ,
819815 );
820816 queryParams.add (qCode);
821- },
822- path: (p) {
817+ case ParameterPath (name: final name, description: final description):
823818 input.add ('required String $pNameCamel ' );
824819 inputDescription.add (
825- "`$pNameCamel `: ${p . description ?? 'No description' }" ,
820+ "`$pNameCamel `: ${description ?? 'No description' }" ,
826821 );
827822 // Update the path definition
828- path = path.replaceAll ('{${p .name }}' , '\$ $pNameCamel ' );
829- },
830- );
823+ path = path.replaceAll ('{$name }' , '\$ $pNameCamel ' );
824+ }
831825 }
832826
833827 // - - - - - - - - - - - - - - -
@@ -861,13 +855,11 @@ class $clientName {
861855 bool isRequestRequired = request.required == true ;
862856
863857 // If a schema is an empty object, ignore
864- rSchema? .mapOrNull (
865- object: (s) {
866- if ((s.properties? .isEmpty ?? false )) {
867- isRequestRequired = false ;
868- }
869- },
870- );
858+ switch (rSchema) {
859+ case SchemaObject (properties: final properties)
860+ when (properties? .isEmpty ?? false ):
861+ isRequestRequired = false ;
862+ }
871863
872864 dType = rSchema? .toDartType (unions: schemaGenerator? .unions);
873865
@@ -926,22 +918,20 @@ class $clientName {
926918 returnType = dType ?? returnType;
927919
928920 // Determine the decode strategy
929- rSchema ? . mapOrNull (
930- object : (s) {
921+ switch (rSchema) {
922+ case SchemaObject (ref : final ref) :
931923 // Handle deserialization of single object
932- if (s. ref != null || returnType.startsWith ('Union' )) {
924+ if (ref != null || returnType.startsWith ('Union' )) {
933925 decoder = "return $returnType .fromJson(_jsonDecode(r));" ;
934926 // Handle deserialization of arrays and maps
935- final sRef = spec.components? .schemas? [s. ref];
927+ final sRef = spec.components? .schemas? [ref];
936928 if (sRef != null ) {
937- sRef. mapOrNull (
938- array : (value) {
929+ switch (sRef) {
930+ case SchemaArray () :
939931 decoder = "return $returnType .from(_jsonDecode(r));" ;
940- },
941- map: (value) {
932+ case SchemaMap ():
942933 decoder = "return $returnType .from(_jsonDecode(r));" ;
943- },
944- );
934+ }
945935 }
946936 } else {
947937 // Just return the whole response and allow user to handle
@@ -950,26 +940,23 @@ class $clientName {
950940 decoder = "return r;" ;
951941 }
952942 }
953- },
954- array: (s) {
943+ case SchemaArray (items: final items):
955944 // Handle deserialization for array of objects
956- if (s. items.ref != null ) {
945+ if (items.ref != null ) {
957946 decoder = """
958947 final list = _jsonDecode(r) as List;
959- return list.map((e) => ${s . items .ref }.fromJson(e)).toList();
948+ return list.map((e) => ${items .ref }.fromJson(e)).toList();
960949 """ ;
961950 }
962- },
963- map: (s) {
951+ case SchemaMap (valueSchema: final valueSchema):
964952 // Handle deserialization for map of objects
965- if (s. valueSchema? .ref != null ) {
953+ if (valueSchema? .ref != null ) {
966954 decoder = """
967955 final map = _jsonDecode(r) as Map<String, dynamic>;
968- return map.map((k, v) => MapEntry(k, ${s . valueSchema ?.ref }.fromJson(v)));
956+ return map.map((k, v) => MapEntry(k, ${valueSchema ?.ref }.fromJson(v)));
969957 """ ;
970958 }
971- },
972- );
959+ }
973960
974961 if (decoder.isEmpty && returnType != 'void' ) {
975962 if (returnType == 'String' ) {
0 commit comments