@ -582,12 +582,28 @@ class OpenAPIBuilder implements Builder {
parameter . schema ! ,
parameter . schema ! ,
) ;
) ;
if ( result . name = = ' String ' & & parameter . schema ? . pattern ! = null ) {
if ( result . name = = ' String ' ) {
code . write ( '''
if ( parameter . schema ? . pattern ! = null ) {
if ( ! RegExp ( r'${parameter.schema!.pattern!}' ) . hasMatch ( $ { _toDartName ( parameter . name ) } ) ) {
code . write ( '''
throw Exception ( ' Invalid value " \$ ${ _toDartName ( parameter . name ) } " for parameter " ${ _toDartName ( parameter . name ) } " with pattern " ' r'${parameter.schema!.pattern!}"' ) ; / / coverage: ignore - line
if ( ! RegExp ( r'${parameter.schema!.pattern!}' ) . hasMatch ( $ { _toDartName ( parameter . name ) } ) ) {
throw Exception ( ' Invalid value " \$ ${ _toDartName ( parameter . name ) } " for parameter " ${ _toDartName ( parameter . name ) } " with pattern " ' r'${parameter.schema!.pattern!}"' ) ; / / coverage: ignore - line
}
''' );
}
if ( parameter . schema ? . minLength ! = null ) {
code . write ( '''
if ( $ { _toDartName ( parameter . name ) } . length < $ { parameter . schema ! . minLength ! } ) {
throw Exception ( ' Parameter " ${ _toDartName ( parameter . name ) } " has to be at least ${ parameter . schema ! . minLength ! } characters long ' ) ; / / coverage: ignore - line
}
''' );
}
if ( parameter . schema ? . maxLength ! = null ) {
code . write ( '''
if ( $ { _toDartName ( parameter . name ) } . length > $ { parameter . schema ! . maxLength ! } ) {
throw Exception ( ' Parameter " ${ _toDartName ( parameter . name ) } " has to be at most ${ parameter . schema ! . maxLength ! } characters long ' ) ; / / coverage: ignore - line
}
''' );
}
}
''' );
}
}
final defaultValueCode = parameter . schema ? . default_ ! = null
final defaultValueCode = parameter . schema ? . default_ ! = null
@ -1082,7 +1098,7 @@ TypeResult resolveObject(
refer ( ' JsonSerializable ' ) . call (
refer ( ' JsonSerializable ' ) . call (
[ ] ,
[ ] ,
{
{
if ( schema . additionalProperties ? ? false ) . . . {
if ( schema . additionalProperties ! = null ) . . . {
' disallowUnrecognizedKeys ' : refer ( ' false ' ) ,
' disallowUnrecognizedKeys ' : refer ( ' false ' ) ,
} ,
} ,
if ( extraJsonSerializableValues ! = null ) . . . {
if ( extraJsonSerializableValues ! = null ) . . . {
@ -1351,7 +1367,7 @@ TypeResult resolveType(
final s = schema . ofs ! [ results . indexOf ( result ) ] ;
final s = schema . ofs ! [ results . indexOf ( result ) ] ;
b
b
. . name = fields [ result . name ]
. . name = fields [ result . name ]
. . type = refer ( _makeNullable ( result . name , true ) )
. . type = refer ( _makeNullable ( result . name , ! ( schema . allOf ? . contains ( s ) ? ? false ) ) )
. . modifier = FieldModifier . final $
. . modifier = FieldModifier . final $
. . docs . addAll ( _descriptionToDocs ( s . description ) ) ;
. . docs . addAll ( _descriptionToDocs ( s . description ) ) ;
} ,
} ,
@ -1374,7 +1390,8 @@ TypeResult resolveType(
( final b ) = > b
( final b ) = > b
. . name = fields [ result . name ] !
. . name = fields [ result . name ] !
. . toThis = true
. . toThis = true
. . named = true ,
. . named = true
. . required = schema . allOf ! = null ,
) ,
) ,
] ,
] ,
] ) ,
] ) ,
@ -1393,48 +1410,54 @@ TypeResult resolveType(
)
)
. . body = Code (
. . body = Code (
< String > [
< String > [
for ( final result in results ) . . . [
if ( schema . allOf ! = null ) . . . [
' ${ result . name } ? ${ fields [ result . name ] ! } ; ' ,
' return ${ state . prefix } $ identifier ( ' ,
] ,
' data, ' ,
for ( final result in results ) . . . [
for ( final result in results ) . . . [
if ( schema . discriminator ! = null ) . . . [
' ${ fields [ result . name ] ! } : ${ result . deserialize ( ' data ' ) } , ' ,
" if (data[' ${ schema . discriminator ! . propertyName } '] == ' ${ result . name . replaceFirst ( state . prefix , ' ' ) } ' " ,
] ,
if ( schema . discriminator ! . mapping ! = null & &
' ); ' ,
schema . discriminator ! . mapping ! . isNotEmpty ) . . . [
] else . . . [
for ( final key in schema . discriminator ! . mapping ! . entries
for ( final result in results ) . . . [
. where (
' ${ result . name } ? ${ fields [ result . name ] ! } ; ' ,
( final entry ) = >
] ,
entry . value . endsWith ( ' / ${ result . name . replaceFirst ( state . prefix , ' ' ) } ' ) ,
for ( final result in results ) . . . [
)
if ( schema . discriminator ! = null ) . . . [
. map ( ( final entry ) = > entry . key ) ) . . . [
" if (data[' ${ schema . discriminator ! . propertyName } '] == ' ${ result . name . replaceFirst ( state . prefix , ' ' ) } ' " ,
" || data[' ${ schema . discriminator ! . propertyName } '] == ' $ key ' " ,
if ( schema . discriminator ! . mapping ! = null & &
schema . discriminator ! . mapping ! . isNotEmpty ) . . . [
for ( final key in schema . discriminator ! . mapping ! . entries
. where (
( final entry ) = >
entry . value . endsWith ( ' / ${ result . name . replaceFirst ( state . prefix , ' ' ) } ' ) ,
)
. map ( ( final entry ) = > entry . key ) ) . . . [
" || data[' ${ schema . discriminator ! . propertyName } '] == ' $ key ' " ,
] ,
] ,
] ,
' ) { ' ,
] ,
' try { ' ,
' ${ fields [ result . name ] ! } = ${ result . deserialize ( ' data ' ) } ; ' ,
' } catch (_) { ' ,
if ( schema . discriminator ! = null ) . . . [
' rethrow; ' ,
] ,
' } ' ,
if ( schema . discriminator ! = null ) . . . [
' } ' ,
] ,
] ,
' ) { ' ,
] ,
] ,
' try { ' ,
if ( schema . oneOf ! = null ) . . . [
' ${ fields [ result . name ] ! } = ${ result . deserialize ( ' data ' ) } ; ' ,
" assert([ ${ fields . values . join ( ' , ' ) } ].where((final x) => x != null).length == 1, 'Need oneOf for \$ data'); " ,
' } catch (_) { ' ,
if ( schema . discriminator ! = null ) . . . [
' rethrow; ' ,
] ,
] ,
' } ' ,
' return ${ state . prefix } $ identifier ( ' ,
if ( schema . discriminator ! = null ) . . . [
' data, ' ,
' } ' ,
for ( final result in results ) . . . [
' ${ fields [ result . name ] ! } : ${ fields [ result . name ] ! } , ' ,
] ,
] ,
' ); ' ,
] ,
] ,
if ( schema . oneOf ! = null ) . . . [
" assert([ ${ fields . values . join ( ' , ' ) } ].where((final x) => x != null).length == 1, 'Need oneOf for \$ data'); " ,
] ,
if ( schema . allOf ! = null ) . . . [
" assert([ ${ fields . values . join ( ' , ' ) } ].where((final x) => x != null).length == ${ fields . length } , 'Need allOf for \$ data'); " ,
] ,
' return ${ state . prefix } $ identifier ( ' ,
' data, ' ,
for ( final result in results ) . . . [
' ${ fields [ result . name ] ! } : ${ fields [ result . name ] ! } , ' ,
] ,
' ); ' ,
] . join ( ) ,
] . join ( ) ,
) ;
) ;
} ,
} ,
@ -1543,6 +1566,27 @@ TypeResult resolveType(
break ;
break ;
case ' object ' :
case ' object ' :
if ( schema . properties = = null ) {
if ( schema . properties = = null ) {
if ( schema . additionalProperties ! = null ) {
if ( schema . additionalProperties is EmptySchema ) {
result = TypeResultMap (
' Map<String, dynamic> ' ,
TypeResultBase ( ' dynamic ' ) ,
) ;
} else {
final subResult = resolveType (
spec ,
state ,
identifier ,
schema . additionalProperties ! ,
extraJsonSerializableValues: extraJsonSerializableValues ,
) ;
result = TypeResultMap (
' Map<String, ${ subResult . name } > ' ,
TypeResultBase ( ' dynamic ' ) ,
) ;
}
break ;
}
result = TypeResultBase ( ' dynamic ' ) ;
result = TypeResultBase ( ' dynamic ' ) ;
break ;
break ;
}
}