@SuppressWarnings({"SimplifyBooleanReturn"})
  public static Map<String, ParamDefinition> cleanupParams(Map<String, ParamDefinition> params) {
    if (params == null || params.isEmpty()) {
      return params;
    }
    Map<String, ParamDefinition> mapped =
        params.entrySet().stream()
            .collect(
                MapHelper.toListMap(
                    Map.Entry::getKey,
                    p -> {
                      ParamDefinition param = p.getValue();
                      if (param.getType() == ParamType.MAP) {
                        MapParamDefinition mapParamDef = param.asMapParamDef();
                        if (mapParamDef.getValue() == null
                            && (mapParamDef.getInternalMode() == InternalParamMode.OPTIONAL)) {
                          return mapParamDef;
                        }
                        return MapParamDefinition.builder()
                            .name(mapParamDef.getName())
                            .value(cleanupParams(mapParamDef.getValue()))
                            .expression(mapParamDef.getExpression())
                            .name(mapParamDef.getName())
                            .validator(mapParamDef.getValidator())
                            .tags(mapParamDef.getTags())
                            .mode(mapParamDef.getMode())
                            .meta(mapParamDef.getMeta())
                            .build();
                      } else {
                        return param;
                      }
                    }));
    Map<String, ParamDefinition> filtered =
        mapped.entrySet().stream()
            .filter(
                p -> {
                  ParamDefinition param = p.getValue();
                  if (param.getInternalMode() == InternalParamMode.OPTIONAL) {
                    if (param.getValue() == null && param.getExpression() == null) {
                      return false;
                    } else if (param.getType() == ParamType.MAP
                        && param.asMapParamDef().getValue() != null
                        && param.asMapParamDef().getValue().isEmpty()) {
                      return false;
                    } else {
                      return true;
                    }
                  } else {
                    Checks.checkTrue(
                        param.getValue() != null || param.getExpression() != null,
                        String.format(
                            "[%s] is a required parameter (type=[%s])",
                            p.getKey(), param.getType()));
                    return true;
                  }
                })
            .collect(MapHelper.toListMap(Map.Entry::getKey, Map.Entry::getValue));
    return cleanIntermediateMetadata(filtered);
  }
@Test
  public void testCleanupOptionalEmptyParams() throws JsonProcessingException {
    Map<String, ParamDefinition> allParams =
        parseParamDefMap("{'optional': {'type': 'STRING', 'internal_mode': 'OPTIONAL'}}");
    Map<String, ParamDefinition> cleanedParams = ParamsMergeHelper.cleanupParams(allParams);
    assertEquals(0, cleanedParams.size());
  }