All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apiaddicts.apitools.dosonarapi.checks.AtMostOneBodyParameterCheck Maven / Gradle / Ivy

The newest version!
/*
 * doSonarAPI: SonarQube OpenAPI Plugin
 * Copyright (C) 2021-2022 Apiaddicts
 * contacta AT apiaddicts DOT org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.apiaddicts.apitools.dosonarapi.checks;

import com.google.common.collect.Sets;
import com.sonar.sslr.api.AstNodeType;
import java.util.Set;
import org.sonar.check.Rule;
import org.apiaddicts.apitools.dosonarapi.api.OpenApiCheck;
import org.apiaddicts.apitools.dosonarapi.api.v2.OpenApi2Grammar;
import org.apiaddicts.apitools.dosonarapi.sslr.yaml.grammar.JsonNode;

@Rule(key = AtMostOneBodyParameterCheck.CHECK_KEY)
public class AtMostOneBodyParameterCheck extends OpenApiCheck {
  public static final String CHECK_KEY = "AtMostOneBodyParameter";

  @Override
  public Set subscribedKinds() {
    return Sets.newHashSet(OpenApi2Grammar.OPERATION);
  }

  @Override
  protected void visitNode(JsonNode node) {
    long params = node.at("/parameters").elements().stream()
        .filter(this::isBodyParam)
        .count();
    if (params > 1) {
      addIssue("An operation can have at most one body parameter.", node.key());
    }
  }

  private boolean isBodyParam(JsonNode n) {
    return n.resolve().at("/in").getTokenValue().equals("body");
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy