diff --git a/packages/openapi_freezed_dio_builder/lib/src/openapi_code_builder.dart b/packages/openapi_freezed_dio_builder/lib/src/openapi_code_builder.dart index 449d312..85d6090 100644 --- a/packages/openapi_freezed_dio_builder/lib/src/openapi_code_builder.dart +++ b/packages/openapi_freezed_dio_builder/lib/src/openapi_code_builder.dart @@ -19,13 +19,13 @@ final _logger = Logger('openapi_code_builder'); class OpenApiLibraryGenerator { OpenApiLibraryGenerator( - this.api, - this.baseName, - this.partFileName, - this.freezedPartFileName, { - this.useNullSafetySyntax = false, - this.apiMethodsWithRequest = false, - }); + this.api, + this.baseName, + this.partFileName, + this.freezedPartFileName, { + this.useNullSafetySyntax = false, + this.apiMethodsWithRequest = false, + }); final APIDocument api; @@ -83,278 +83,281 @@ class OpenApiLibraryGenerator { final grouped = api.paths!.entries.groupListsBy((element) => element.value!.operations.values.map((e) => e!.tags ?? []).expand((element) => element).toSet().join()); final classes = grouped.entries.map((value) => Class((cb) { - cb - ..name = '${value.key.titleCase.replaceAll(' ', '')}Api' - ..constructors.add(Constructor( + cb + ..name = '${value.key.titleCase.replaceAll(' ', '')}Api' + ..constructors.add(Constructor( (cb) => cb - ..requiredParameters.addAll(fields.map((f) => Parameter((pb) => pb - ..name = f.key - ..toThis = true))), - )) - ..fields.addAll(fields.map((f) => Field((fb) => fb + ..requiredParameters.addAll(fields.map((f) => Parameter((pb) => pb ..name = f.key - ..type = f.value - ..modifier = FieldModifier.final$))); - //cb.name = baseName; - cb.abstract = false; - for (final path in value.value) { - for (final operation in path.value!.operations.entries) { - final pathName = path.key - .replaceAll( - // language=RegExp - RegExp(r'[{}]'), - '') - .camelCase; - final operationName = operation.value!.id == null - ? '$pathName' - '${operation.key.pascalCase}' - : operation.value!.id!.camelCase; - - final responseClass = ClassBuilder(); - responseClass - ..name = '${operationName.pascalCase}Response' - ..abstract = true - ..constructors.add(Constructor()); - Reference? successResponseBodyType; - for (final response in operation.value!.responses!.entries) { - final clientResponseParseParams = []; - final content = (response.value!.content ?? {})[mediaTypeJson.contentType]; + ..toThis = true))), + )) + ..fields.addAll(fields.map((f) => Field((fb) => fb + ..name = f.key + ..type = f.value + ..modifier = FieldModifier.final$))); + //cb.name = baseName; + cb.abstract = false; + for (final path in value.value) { + for (final operation in path.value!.operations.entries) { + final pathName = path.key + .replaceAll( + // language=RegExp + RegExp(r'[{}]'), + '') + .camelCase; + final operationName = operation.value!.id == null + ? '$pathName' + '${operation.key.pascalCase}' + : operation.value!.id!.camelCase; + + final responseClass = ClassBuilder(); + responseClass + ..name = '${operationName.pascalCase}Response' + ..abstract = true + ..constructors.add(Constructor()); + Reference? successResponseBodyType; + for (final response in operation.value!.responses!.entries) { + final clientResponseParseParams = []; + final content = (response.value!.content ?? {})[mediaTypeJson.contentType]; // OpenApiContentType responseContentType; - Reference? bodyType; - if (content != null) { - if (content.schema!.type == APIType.array) { - bodyType = _referType('List', generics: [ - _schemaReference(content.schema!.items!.referenceURI!.pathSegments.last, content.schema!) - ]); - } else { - bodyType = _schemaReference(responseClass.name!, content.schema!); - } + Reference? bodyType; + if (content != null) { + if (content.schema!.type == APIType.array) { + bodyType = _referType('List', generics: [ + _schemaReference(content.schema!.items!.referenceURI!.pathSegments.last, content.schema!) + ]); + } else { + bodyType = _schemaReference(responseClass.name!, content.schema!); + } - clientResponseParseParams.add(bodyType - .newInstanceNamed('fromJson', [refer('response').property('responseBodyJson')([]).awaited])); + clientResponseParseParams.add(bodyType + .newInstanceNamed('fromJson', [refer('response').property('responseBodyJson')([]).awaited])); + } else { + if (response.value!.content?.length == 1) { + final responseContent = response.value!.content!.entries.first; + bodyType = _toDartType('${refer(responseClass.name!)}Content', responseContent.value!.schema!); + if (_typeString == bodyType) { + clientResponseParseParams.add(refer('response').property('responseBodyString')([]).awaited); + } else if (_dioResponseBody == bodyType) { + clientResponseParseParams.add(refer('response').property('responseBodyBytes')([]).awaited); } else { - if (response.value!.content?.length == 1) { - final responseContent = response.value!.content!.entries.first; - bodyType = _toDartType('${refer(responseClass.name!)}Content', responseContent.value!.schema!); - if (_typeString == bodyType) { - clientResponseParseParams.add(refer('response').property('responseBodyString')([]).awaited); - } else if (_dioResponseBody == bodyType) { - clientResponseParseParams.add(refer('response').property('responseBodyBytes')([]).awaited); - } else { - throw StateError('Unsupported bodyType $bodyType for responses.'); - } - } - } - if (response.key.startsWith('2') || (response.key == 'default' && successResponseBodyType == null)) { - successResponseBodyType = bodyType; + throw StateError('Unsupported bodyType $bodyType for responses.'); } } + } + if (response.key.startsWith('2') || (response.key == 'default' && successResponseBodyType == null)) { + successResponseBodyType = bodyType; + } + } - final generics = [_dioResponse.addGenerics(successResponseBodyType ?? _void)]; - - //lb.body.add(responseClass.build()); - final clientMethod = MethodBuilder() - ..name = operationName - ..addDartDoc(operation.value!.summary) - ..addDartDoc(operation.value!.description) - ..docs.add('/// ${operation.key}: ${path.key}') - ..docs.add('///') - ..returns = _referType('Future', generics: generics) - ..modifier = MethodModifier.async; - final clientCode = []; - - Method((mb) { - mb - ..name = operationName - ..addDartDoc(operation.value!.summary) - ..addDartDoc(operation.value!.description) - ..docs.add('/// ${operation.key}: ${path.key}') - ..returns = _referType('Future', generics: generics); - - final routerParams = []; - final routerParamsNamed = {}; - - if (apiMethodsWithRequest) { - mb.requiredParameters.add(Parameter((pb) => pb..name = 'request')); - routerParams.add(refer('request')); - } + final generics = [_dioResponse.addGenerics(successResponseBodyType ?? _void)]; + + //lb.body.add(responseClass.build()); + final clientMethod = MethodBuilder() + ..name = operationName + ..addDartDoc(operation.value!.summary) + ..addDartDoc(operation.value!.description) + ..docs.add('/// ${operation.key}: ${path.key}') + ..docs.add('///') + ..returns = _referType('Future', generics: generics) + ..modifier = MethodModifier.async; + final clientCode = []; + + Method((mb) { + mb + ..name = operationName + ..addDartDoc(operation.value!.summary) + ..addDartDoc(operation.value!.description) + ..docs.add('/// ${operation.key}: ${path.key}') + ..returns = _referType('Future', generics: generics); + + final routerParams = []; + final routerParamsNamed = {}; + + if (apiMethodsWithRequest) { + mb.requiredParameters.add(Parameter((pb) => pb..name = 'request')); + routerParams.add(refer('request')); + } - // ignore: avoid_function_literals_in_foreach_calls - final allParameters = [...?path.value!.parameters, ...?operation.value!.parameters]; - for (final param in allParameters) { - final paramType = _toDartType(operationName, param!.schema!); - final paramNameCamelCase = param.name!.camelCase; - if (param.description != null) { - clientMethod.docs.add('/// * [$paramNameCamelCase]: ${param.description}'); - } - final p = Parameter((pb) => pb - ..name = paramNameCamelCase - ..type = paramType.asNullable(!param.isRequired) - ..asRequired(this, param.isRequired) - ..named = true); - mb.optionalParameters.add(p); - clientMethod.optionalParameters.add(p); - final decodeParameterFrom = (APIParameter param, Expression expression) { - final schemaType = ArgumentError.checkNotNull(param.schema?.type, 'param.schema.type'); - switch (schemaType) { - case APIType.string: - final Expression? asString = refer('paramToString')([expression]); - if (param.schema!.format == 'uuid') { - assert(paramType == _apiUuid); - return _apiUuid.newInstanceNamed('parse', [asString!]); - } else if (paramType != _typeString) { - //throw StateError('Unsupported paramType for string $paramType'); - return _apiUuid.newInstanceNamed('parse', [asString!]); - } - return asString; - case APIType.number: - break; - case APIType.integer: - return refer('paramToInt')([expression]); - case APIType.boolean: - return refer('paramToBool')([expression]); - case APIType.array: - //checkState(param.schema!.items!.type == APIType.string); - if (param.schema!.items!.enumerated != null && param.schema!.items!.enumerated!.isNotEmpty) { - final paramEnumType = (paramType as TypeReference).types.first; - return expression - .property('map')([ - Method( - (mb) => mb - ..lambda = true - ..requiredParameters.add(Parameter((pb) => pb..name = 'e')) - ..body = - refer(paramEnumType.symbol! + 'Ext').property('fromName')([refer('e')]).code, - ).closure - ]) - .property('toList')([]); - } else { - final paramEnumType = (paramType as TypeReference).types.first; - return refer('paramToInt')([expression]); - return expression - .property('map')([ - Method( - (mb) => mb - ..lambda = true - ..requiredParameters.add(Parameter((pb) => pb..name = 'e')) - ..body = refer('e').code, - ).closure - ]) - .property('toList')([]); - } - return expression; - case APIType.object: - return expression; - default: - throw StateError('Invalid schema type $schemaType'); + // ignore: avoid_function_literals_in_foreach_calls + final allParameters = [...?path.value!.parameters, ...?operation.value!.parameters]; + for (final param in allParameters) { + final paramType = _toDartType(operationName, param!.schema!); + final paramNameCamelCase = param.name!.camelCase; + if (param.description != null) { + clientMethod.docs.add('/// * [$paramNameCamelCase]: ${param.description}'); + } + final p = Parameter((pb) => pb + ..name = paramNameCamelCase + ..type = paramType.asNullable(!param.isRequired) + ..asRequired(this, param.isRequired) + ..named = true); + mb.optionalParameters.add(p); + clientMethod.optionalParameters.add(p); + final decodeParameterFrom = (APIParameter param, Expression expression) { + final schemaType = ArgumentError.checkNotNull(param.schema?.type, 'param.schema.type'); + switch (schemaType) { + case APIType.string: + final Expression? asString = refer('paramToString')([expression]); + if (param.schema!.format == 'uuid') { + assert(paramType == _apiUuid); + return _apiUuid.newInstanceNamed('parse', [asString!]); + } else if (paramType != _typeString) { + //throw StateError('Unsupported paramType for string $paramType'); + return _apiUuid.newInstanceNamed('parse', [asString!]); } - }; - final decodeParameter = (Expression? expression) { - return refer(param.isRequired ? 'paramRequired' : 'paramOpt')([], { - 'name': literalString(param.name!), - 'value': expression!, - 'decode': Method((mb) { - final paramFrom = decodeParameterFrom(param, refer('value')); - mb - ..lambda = true - ..requiredParameters.add(Parameter((pb) => pb..name = 'value')) - ..body = paramFrom?.code; - }).closure, - }); - }; - final paramLocation = ArgumentError.checkNotNull(param.location); - final paramName = ArgumentError.checkNotNull(param.name); - routerParamsNamed[paramNameCamelCase] = decodeParameter(_readFromRequest(paramLocation, paramName)); + return asString; + case APIType.number: + break; + case APIType.integer: + return refer('paramToInt')([expression]); + case APIType.boolean: + return refer('paramToBool')([expression]); + case APIType.array: + //checkState(param.schema!.items!.type == APIType.string); + if (param.schema!.items!.enumerated != null && param.schema!.items!.enumerated!.isNotEmpty) { + final paramEnumType = (paramType as TypeReference).types.first; + return expression + .property('map')([ + Method( + (mb) => mb + ..lambda = true + ..requiredParameters.add(Parameter((pb) => pb..name = 'e')) + ..body = + refer(paramEnumType.symbol! + 'Ext').property('fromName')([refer('e')]).code, + ).closure + ]) + .property('toList')([]); + } else { + final paramEnumType = (paramType as TypeReference).types.first; + return refer('paramToInt')([expression]); + return expression + .property('map')([ + Method( + (mb) => mb + ..lambda = true + ..requiredParameters.add(Parameter((pb) => pb..name = 'e')) + ..body = refer('e').code, + ).closure + ]) + .property('toList')([]); + } + return expression; + case APIType.object: + return expression; + default: + throw StateError('Invalid schema type $schemaType'); } + }; + final decodeParameter = (Expression? expression) { + return refer(param.isRequired ? 'paramRequired' : 'paramOpt')([], { + 'name': literalString(param.name!), + 'value': expression!, + 'decode': Method((mb) { + final paramFrom = decodeParameterFrom(param, refer('value')); + mb + ..lambda = true + ..requiredParameters.add(Parameter((pb) => pb..name = 'value')) + ..body = paramFrom?.code; + }).closure, + }); + }; + final paramLocation = ArgumentError.checkNotNull(param.location); + final paramName = ArgumentError.checkNotNull(param.name); + routerParamsNamed[paramNameCamelCase] = decodeParameter(_readFromRequest(paramLocation, paramName)); + } - final body = operation.value!.requestBody; - if (body != null && body.content!.isNotEmpty) { - final entry = body.content!.entries.first; + final body = operation.value!.requestBody; + if (body != null && body.content!.isNotEmpty) { + final entry = body.content!.entries.first; - if (body.content!.length > 1) { - _logger.warning('Right now we only support one request body, ' - 'but found: ${body.content!.keys}, only using $entry'); - } + if (body.content!.length > 1) { + _logger.warning('Right now we only support one request body, ' + 'but found: ${body.content!.keys}, only using $entry'); + } - Map.fromEntries([entry]).forEach((key, reqBody) { - final contentType = OpenApiContentType.parse(key); + Map.fromEntries([entry]).forEach((key, reqBody) { + final contentType = OpenApiContentType.parse(key); // final ct = OpenApiContentType.allKnown // .firstWhere((element) => element.matches(contentType)); - _createRequestBody( - contentType, - reqBody!, - body.isRequired, - operationName, - mb, - routerParams, - clientMethod, - clientCode, - ); - }); - } + _createRequestBody( + contentType, + reqBody!, + body.isRequired, + operationName, + mb, + routerParams, + clientMethod, + clientCode, + ); }); + } + }); + + clientCode.add(Code('final queryParams = {};')); + + final allParameters = [ + ...?operation.value?.parameters?.where((element) => element?.location == APIParameterLocation.query), + ...?path.value?.parameters?.where((element) => element?.location == APIParameterLocation.query), + ]; + for (final element in allParameters) { + if (element?.schema?.type == APIType.array) { + clientCode.add(Code( + '''if (${element!.name!.camelCase} != null) queryParams['${element.name}'] = ${element.name!.camelCase}.join(',');''')); + } else if (element?.schema?.enumerated != null) { + clientCode.add(Code( + '''if (${element!.name!.camelCase} != null) queryParams['${element.name}'] = ${element.name!.camelCase}.name;''')); + } else { + clientCode.add(Code( + '''if (${element!.name!.camelCase} != null) queryParams['${element.name}'] = ${element.name!.camelCase}.toString();''')); + } + } - clientCode.add(Code('final queryParams = {};')); - - final allParameters = [ - ...?operation.value?.parameters?.where((element) => element?.location == APIParameterLocation.query), - ...?path.value?.parameters?.where((element) => element?.location == APIParameterLocation.query), - ]; - for (final element in allParameters) { - if (element?.schema?.type == APIType.array) { - clientCode.add(Code( - '''if (${element!.name!.camelCase} != null) queryParams['${element.name}'] = ${element.name!.camelCase}.join(',');''')); - } else if (element?.schema?.enumerated != null) { - clientCode.add(Code( - '''if (${element!.name!.camelCase} != null) queryParams['${element.name}'] = ${element.name!.camelCase}.name;''')); - } else { - clientCode.add(Code( - '''if (${element!.name!.camelCase} != null) queryParams['${element.name}'] = ${element.name!.camelCase}.toString();''')); - } - } - - clientCode.add(Code('''final uri = baseUri.replace( - queryParameters: queryParams, path: baseUri.path + '${path.key.replaceAll('{', '\${')}');''')); - - if (successResponseBodyType == _dioResponseBody) { - clientCode.add( - Code( - 'final response = await dio.${operation.key}Uri<${successResponseBodyType!.symbol}>(uri${operation.value?.requestBody != null ? ', data: body' : ''}, options: Options(responseType: ResponseType.stream));'), - ); - } else if (successResponseBodyType?.symbol == 'List') { - clientCode.add( - Code( - 'final response = await dio.${operation.key}Uri<${successResponseBodyType != null ? 'List' : 'void'}>(uri${operation.value?.requestBody != null ? ', data: body' : ''});'), - ); - } else if (operation.value!.requestBody?.content?.keys.firstOrNull == 'multipart/form-data') { - final body = '''FormData.fromMap({ + clientCode.add(Code(''' + final uri = baseUri.replace( + queryParameters: queryParams, + path: baseUri.path + '${_convertPathParamsToCamelCase(path.key.replaceAll('{', '\${'))}'); + ''')); + + if (successResponseBodyType == _dioResponseBody) { + clientCode.add( + Code( + 'final response = await dio.${operation.key}Uri<${successResponseBodyType!.symbol}>(uri${operation.value?.requestBody != null ? ', data: body' : ''}, options: Options(responseType: ResponseType.stream));'), + ); + } else if (successResponseBodyType?.symbol == 'List') { + clientCode.add( + Code( + 'final response = await dio.${operation.key}Uri<${successResponseBodyType != null ? 'List' : 'void'}>(uri${operation.value?.requestBody != null ? ', data: body' : ''});'), + ); + } else if (operation.value!.requestBody?.content?.keys.firstOrNull == 'multipart/form-data') { + final body = '''FormData.fromMap({ ${operation.value!.requestBody!.content!.values.first!.schema!.properties!.entries.map((element) => '\'${element.key}\': ${element.value!.type == APIType.object ? 'MultipartFile.fromString(jsonEncode(' + element.key + '.toJson()), filename: \'' + element.key + '.json\', contentType: MediaType(\'application\', \'json\'))' : element.key}').join(',')} })'''; - clientCode.add( - Code( - 'final response = await dio.${operation.key}Uri<${successResponseBodyType != null ? 'Map' : 'void'}>(uri${operation.value?.requestBody != null ? ', data: $body' : ''});'), - ); - } else { - // TODO list all primitives? - if (successResponseBodyType == _typeString) { - clientCode.add( - Code( - 'final response = await dio.${operation.key}Uri<${successResponseBodyType!.symbol}>(uri${operation.value?.requestBody != null ? ', data: body' : ''});'), - ); - } else { - clientCode.add( - Code( - 'final response = await dio.${operation.key}Uri<${successResponseBodyType != null ? 'Map' : 'void'}>(uri${operation.value?.requestBody != null ? ', data: body' : ''});'), - ); - } - } - if (successResponseBodyType == null || successResponseBodyType == _dioResponseBody) { - clientCode.add(Code('return response;')); - } else if (successResponseBodyType.symbol == 'List') { - final listType = (successResponseBodyType as TypeReference).types.last; - clientCode.add( - Code( - '''final parsed = response.data!.map((dynamic e) => ${listType.symbol}.fromJson(e as Map)).toList(); + clientCode.add( + Code( + 'final response = await dio.${operation.key}Uri<${successResponseBodyType != null ? 'Map' : 'void'}>(uri${operation.value?.requestBody != null ? ', data: $body' : ''});'), + ); + } else { + // TODO list all primitives? + if (successResponseBodyType == _typeString) { + clientCode.add( + Code( + 'final response = await dio.${operation.key}Uri<${successResponseBodyType!.symbol}>(uri${operation.value?.requestBody != null ? ', data: body' : ''});'), + ); + } else { + clientCode.add( + Code( + 'final response = await dio.${operation.key}Uri<${successResponseBodyType != null ? 'Map' : 'void'}>(uri${operation.value?.requestBody != null ? ', data: body' : ''});'), + ); + } + } + if (successResponseBodyType == null || successResponseBodyType == _dioResponseBody) { + clientCode.add(Code('return response;')); + } else if (successResponseBodyType.symbol == 'List') { + final listType = (successResponseBodyType as TypeReference).types.last; + clientCode.add( + Code( + '''final parsed = response.data!.map((dynamic e) => ${listType.symbol}.fromJson(e as Map)).toList(); return Response<${successResponseBodyType.symbol}<${listType.symbol}>>( data: parsed, headers: response.headers, @@ -365,11 +368,11 @@ class OpenApiLibraryGenerator { extra: response.extra, ); '''), - ); - } else { - clientCode.add( - Code( - '''final parsed = ${successResponseBodyType == _typeString ? 'response.data!' : '${successResponseBodyType.symbol}.fromJson(response.data!)'}; + ); + } else { + clientCode.add( + Code( + '''final parsed = ${successResponseBodyType == _typeString ? 'response.data!' : '${successResponseBodyType.symbol}.fromJson(response.data!)'}; return Response<${successResponseBodyType.symbol}>( data: parsed, headers: response.headers, @@ -380,14 +383,14 @@ class OpenApiLibraryGenerator { extra: response.extra, ); '''), - ); - } - - clientMethod.body = Block.of(clientCode); - cb.methods.add(clientMethod.build()); - } + ); } - })); + + clientMethod.body = Block.of(clientCode); + cb.methods.add(clientMethod.build()); + } + } + })); for (final element in classes) { lb.body.add(element); } @@ -395,6 +398,17 @@ class OpenApiLibraryGenerator { return lb.build(); } + String _convertPathParamsToCamelCase(String path) { + final regex = RegExp(r'{(.*?)}'); + return path.replaceAllMapped(regex, (match) { + final param = match.group(1); + if (param != null) { + return '{${param.snakeCase.camelCase}}'; + } + return match.group(0)!; + }); + } + Expression _readFromRequest(APIParameterLocation location, String name) { switch (location) { case APIParameterLocation.query: @@ -479,8 +493,8 @@ class OpenApiLibraryGenerator { final mapExpression = contentType.matches(OpenApiContentType.json) ? refer('request').property('readJsonBody')([]).awaited : (contentType.matches(OpenApiContentType.urlencoded) - ? refer('request').property('readUrlEncodedBodyFlat')([]).awaited - : literalConstMap({}, refer('String'), refer('dynamic'))); + ? refer('request').property('readUrlEncodedBodyFlat')([]).awaited + : literalConstMap({}, refer('String'), refer('dynamic'))); _addRequestBody( reference, reference.property('fromJson')( @@ -561,20 +575,20 @@ class OpenApiLibraryGenerator { } final fields = properties.map((key, e) => MapEntry(key, Field((fb) { - final fieldType = _toDartType('$className${key.pascalCase}', e!); - fb - ..addDartDoc(e.description) - ..annotations.add(jsonKey([], {'name': literalString(key)})) - ..annotations.addAll(override.contains(key) ? [_override] : []) - ..name = key.camelCase - ..modifier = FieldModifier.final$ - ..type = fieldType.asNullable(!required.contains(key) && e.defaultValue == null); - if (fieldType == _apiUuid) { - fb.annotations.add(_apiUuidNullJsonConverter([])); - } else if (fieldType is TypeReference && fieldType.types.firstOrNull == _apiUuid) { - fb.annotations.add(_listApiUuidNullJsonConverter([])); - } - }))); + final fieldType = _toDartType('$className${key.pascalCase}', e!); + fb + ..addDartDoc(e.description) + ..annotations.add(jsonKey([], {'name': literalString(key)})) + ..annotations.addAll(override.contains(key) ? [_override] : []) + ..name = key.camelCase + ..modifier = FieldModifier.final$ + ..type = fieldType.asNullable(!required.contains(key) && e.defaultValue == null); + if (fieldType == _apiUuid) { + fb.annotations.add(_apiUuidNullJsonConverter([])); + } else if (fieldType is TypeReference && fieldType.types.firstOrNull == _apiUuid) { + fb.annotations.add(_listApiUuidNullJsonConverter([])); + } + }))); // ignore: avoid_function_literals_in_foreach_calls required.forEach((element) { if (fields[element] == null) { @@ -585,16 +599,16 @@ class OpenApiLibraryGenerator { if (false && api.components!.schemas!.entries.any( - (element) => element.value!.allOf?.any((el) => el!.referenceURI.toString().contains(className)) == true)) { + (element) => element.value!.allOf?.any((el) => el!.referenceURI.toString().contains(className)) == true)) { return Mixin((mixing) { mixing ..name = className ..methods.addAll(fields.values.map((e) => Method((m) { - m - ..type = MethodType.getter - ..returns = e.type?.asNullable(true) - ..name = e.name; - }))) + m + ..type = MethodType.getter + ..returns = e.type?.asNullable(true) + ..name = e.name; + }))) ..implements.addAll(implements); }); } @@ -630,11 +644,11 @@ class OpenApiLibraryGenerator { ..annotations.add(freezed) ..name = className ..mixins.add(refer('_\$$className')) - //..mixins.addAll(implements) + //..mixins.addAll(implements) ..docs.addDartDoc(obj.description) ..constructors.add( Constructor( - (cb) => cb + (cb) => cb ..factory = true ..redirect = refer('_$className') ..optionalParameters.addAll(fields.entries.map((f) => Parameter((pb) => pb @@ -651,8 +665,8 @@ class OpenApiLibraryGenerator { return that; }) ..asRequired(this, required.contains(f.key)) - // TODO add default values - //..defaultTo = (properties[f.key]?.defaultValue as Object?)?.let((dynamic it) => literal(it)).code + // TODO add default values + //..defaultTo = (properties[f.key]?.defaultValue as Object?)?.let((dynamic it) => literal(it)).code ..named = true ..type = _toDartType('$className${f.key.pascalCase}', properties[f.key]!).asNullable(true) ..toThis = false))) @@ -687,12 +701,12 @@ class OpenApiLibraryGenerator { values: values! .map( (dynamic e) => EnumValueSpec( - annotations: [ - jsonValue([literalString(e.toString())]) - ], - name: e.toString(), - ), - ) + annotations: [ + jsonValue([literalString(e.toString())]) + ], + name: e.toString(), + ), + ) .toList(), )); return refer(name); @@ -870,8 +884,8 @@ class OpenApiCodeBuilder extends Builder { @override Map> get buildExtensions => { - '.openapi.yaml': ['.openapi.dart'] - }; + '.openapi.yaml': ['.openapi.dart'] + }; } TypeReference _referType(String name, {String? url, List? generics}) => TypeReference((trb) => trb