finalize method Null safety

Future<Map<String, dynamic>> finalize()

Finalizes document and returns it as a serializable Map.

This method is invoked by the command line tool for creating OpenAPI documents.

Implementation

Future<Map<String, dynamic>> finalize() async {
  final dops = _deferredOperations;
  _deferredOperations = [];
  await Future.forEach(dops, (Function dop) => dop());

  document.paths!.values
      .expand((p) => p!.operations.values)
      .where((op) => op!.security != null)
      .expand((op) => op!.security!)
      .forEach((req) {
    req!.requirements!.forEach((schemeName, scopes) {
      final scheme = document.components!.securitySchemes[schemeName];
      if (scheme!.type == APISecuritySchemeType.oauth2) {
        for (final flow in scheme.flows!.values) {
          for (final scope in scopes) {
            if (!flow!.scopes!.containsKey(scope)) {
              flow.scopes![scope] = "";
            }
          }
        }
      }
    });
  });

  return document.asMap();
}