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

io.takari.swagger.v12.Operation Maven / Gradle / Ivy

The newest version!
package io.takari.swagger.v12;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Operation {
  private Method method;
  private String nickname;
  private String summary;
  private String notes;
  private String responseClass;
  private String type = "void";
  private final Set produces = new HashSet();
  private final Set consumes = new HashSet();
  private final List parameters = new ArrayList();
  private final List responseMessages = new ArrayList();

  public enum Method {
    GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH
  }

  public Operation(String nickname, Method method, String description) {
    this.nickname = nickname;
    this.method = method;
    this.summary = description;
    
    // If the is a nickname of "models" it breaks the Swagger app with:
    // TypeError: undefined is not a function (evaluating 'model.setReferencedModels(this.models)')
    if(this.nickname.equals("models")) {
      this.nickname = "XXX";
    }
  }

  public Method getMethod() {
    return method;
  }

  public void setMethod(Method method) {
    assert method != null : "method can not be null";
    this.method = method;
  }

  public String getNickname() {
    return nickname;
  }

  public void setNickname(String nickName) {
    assert nickName != null : "nickName can not be null";
    this.nickname = nickName;
  }

  public String getResponseClass() {
    return responseClass == null ? "void" : responseClass;
  }

  public void setResponseClass(String responseClass) {
    this.responseClass = responseClass;
  }

  public String getSummary() {
    return summary == null ? "" : summary;
  }

  public void setSummary(String summary) {
    this.summary = summary;
  }

  public String getNotes() {
    return notes;
  }

  public void setNotes(String notes) {
    this.notes = notes;
  }

  public Collection getProduces() {
    return Collections.unmodifiableCollection(produces);
  }

  public void removeProduces(String produces) {
    this.produces.remove(produces);
  }

  public void addProduces(String produces) {
    assert produces != null : "produces can not be null";

    this.produces.add(produces);
  }

  public Collection getConsumes() {
    return Collections.unmodifiableCollection(consumes);
  }

  public void removeConsumes(String consumes) {
    this.produces.remove(consumes);
  }

  public void addConsumes(String consumes) {
    assert consumes != null : "consumes can not be null";
    this.consumes.add(consumes);
  }

  public List getParameters() {
    return Collections.unmodifiableList(parameters);
  }

  public Parameter getParameter(String name) {
    assert name != null : "parameter name can not be null";

    synchronized (parameters) {
      for (Parameter parameter : parameters) {
        if (parameter.getName().equals(name)) {
          return parameter;
        }
      }

      return null;
    }
  }

  public void removeParameter(Parameter parameter) {
    assert parameter != null : "parameter can not be null";

    synchronized (parameters) {
      parameters.remove(parameter);
    }
  }

  public Parameter addParameter(String name, String type, Parameter.ParamType parameterType, String description) {
    assert type != null : "parameter must be created with type";
    assert description != null : "parameter must be created with a description";

    if (parameterType != Parameter.ParamType.body) {
      assert name != null : "parameter that is not a body must have a name";
      assert getParameter(name) == null : "Parameter already exists with name [" + name + "]";
    }

    synchronized (parameters) {
      Parameter parameter = new Parameter(name, type, parameterType, description);
      parameters.add(parameter);

      return parameter;
    }
  }

  public List getResponseMessages() {
    return Collections.unmodifiableList(responseMessages);
  }

  public ResponseMessage getResponseMessage(int code) {
    assert code > 0 : "code can not be 0";

    synchronized (responseMessages) {
      for (ResponseMessage responseMessage : responseMessages) {
        if (responseMessage.getCode() == code) {
          return responseMessage;
        }
      }

      return null;
    }
  }

  public void removeResponseMessage(ResponseMessage responseMessage) {
    assert responseMessage != null;

    synchronized (responseMessages) {
      responseMessages.remove(responseMessage);
    }
  }

  public ResponseMessage addResponseMessage(int code, String message) {
    assert code > 0 : "code must have a value";
    assert getResponseMessage(code) == null : "Response for already exists for code [" + code + "]";

    synchronized (responseMessages) {
      ResponseMessage responseMessage = new ResponseMessageImpl(code, message);
      responseMessages.add(responseMessage);
      return responseMessage;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy