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

org.openapi4j.parser.model.AbsOpenApiSchema Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package org.openapi4j.parser.model;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openapi4j.core.exception.EncodeException;
import org.openapi4j.core.model.OAIContext;
import org.openapi4j.core.util.TreeUtil;

import java.util.*;

import static org.openapi4j.core.util.TreeUtil.ENCODE_ERR_MSG;

public abstract class AbsOpenApiSchema> implements OpenApiSchema {
  protected static class Views {
    private Views() {
    }

    public static class Public {
      private Public() {
      }
    }

    public static class Internal extends Public {
      private Internal() {
      }
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public JsonNode toNode(OAIContext context, boolean followRefs) throws EncodeException {
    OpenApiSchema model
      = followRefs
      ? copy(context, true)
      : this;

    try {
      byte[] content = TreeUtil.json
        .writerWithView(Views.Public.class)
        .writeValueAsBytes(model);

      return TreeUtil.json.readTree(content);
    } catch (Exception e) {
      throw new EncodeException(String.format(ENCODE_ERR_MSG, e.getMessage()));
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString(OAIContext context, EnumSet flags) throws EncodeException {
    OpenApiSchema model
      = flags.contains(SerializationFlag.FOLLOW_REFS)
      ? copy(context, true)
      : this;

    ObjectMapper mapper
      = flags.contains(SerializationFlag.OUT_AS_YAML)
      ? TreeUtil.yaml
      : TreeUtil.json;

    try {
      return mapper
        .writerWithView(Views.Public.class)
        .writeValueAsString(model);
    } catch (Exception e) {
      throw new EncodeException(String.format(ENCODE_ERR_MSG, e.getMessage()));
    }
  }

  protected  List copyList(List original) {
    if (original != null) {
      return new ArrayList<>(original);
    }

    return null;
  }

  protected  Map copyMap(Map original) {
    if (original != null) {
      return new LinkedHashMap<>(original);
    }

    return null;
  }

  protected > T copyField(T original, OAIContext context, boolean followRefs) {
    if (original != null) {
      return original.copy(context, followRefs);
    }

    return null;
  }

  protected > List copyList(List original, OAIContext context, boolean followRefs) {
    if (original != null) {
      List copy = new ArrayList<>(original.size());
      for (T element : original) {
        copy.add(element.copy(context, followRefs));
      }

      return copy;
    }

    return null;
  }

  protected > Map copyMap(Map original, OAIContext context, boolean followRefs) {
    if (original != null) {
      Map copy = new LinkedHashMap<>(original.size());
      for (Map.Entry element : original.entrySet()) {
        OpenApiSchema schema = element.getValue();
        if (schema != null) {
          copy.put(element.getKey(), schema.copy(context, followRefs));
        } else {
          copy.put(element.getKey(), null);
        }

      }

      return copy;
    }

    return null;
  }

  protected  Map mapPut(Map map, K key, V value) {
    if (map == null) {
      map = new LinkedHashMap<>();
    }
    map.put(key, value);

    return map;
  }

  protected  boolean mapHas(Map map, K key) {
    return map != null && map.containsKey(key);
  }

  protected  V mapGet(Map map, K key) {
    if (map == null) {
      return null;
    }
    return map.get(key);
  }

  protected  void mapRemove(Map map, K key) {
    if (map != null) {
      map.remove(key);
    }
  }

  protected  List listAdd(List list, K value) {
    if (list == null) {
      list = new ArrayList<>();
    }
    list.add(value);

    return list;
  }

  protected  List listAdd(List list, int index, K value) {
    if (list == null) {
      list = new ArrayList<>();
    }
    list.add(index, value);

    return list;
  }

  protected  boolean listRemove(List list, K value) {
    if (list == null) return false;

    return list.remove(value);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy