public SecuritySchemeDefinition securityDefinition(
      ObjectNode node, String location, ParseResult result) {
    if (node == null) return null;

    SecuritySchemeDefinition output = null;

    String type = getString("type", node, true, location, result);
    if (type != null) {
      if (type.equals("basic")) {
        // TODO: parse manually for better feedback
        output = Json.mapper().convertValue(node, BasicAuthDefinition.class);
      } else if (type.equals("apiKey")) {
        String position = getString("in", node, true, location, result);
        String name = getString("name", node, true, location, result);

        if (name != null && ("header".equals(position) || "query".equals(position))) {
          In in = In.forValue(position);
          if (in != null) {
            ApiKeyAuthDefinition auth = new ApiKeyAuthDefinition().name(name).in(in);
            output = auth;
          }
        }
      } else if (type.equals("oauth2")) {
        // TODO: parse manually for better feedback
        output = Json.mapper().convertValue(node, OAuth2Definition.class);
      } else {
        result.invalidType(location + ".type", "type", "basic|apiKey|oauth2", node);
      }
    }

    return output;
  }
  public Response response(ObjectNode node, String location, ParseResult result) {
    if (node == null) return null;

    Response output = new Response();
    JsonNode ref = node.get("$ref");
    if (ref != null) {
      if (ref.getNodeType().equals(JsonNodeType.STRING)) {
        return refResponse((TextNode) ref, location, result);
      } else {
        result.invalidType(location, "$ref", "string", node);
        return null;
      }
    }

    String value = getString("description", node, true, location, result);
    output.description(value);

    ObjectNode schema = getObject("schema", node, false, location, result);
    if (schema != null) {
      output.schema(Json.mapper().convertValue(schema, Property.class));
    }
    ObjectNode headersNode = getObject("headers", node, false, location, result);
    if (headersNode != null) {
      // TODO
      Map<String, Property> headers =
          Json.mapper()
              .convertValue(
                  headersNode,
                  Json.mapper()
                      .getTypeFactory()
                      .constructMapType(Map.class, String.class, Property.class));
      output.headers(headers);
    }

    ObjectNode examplesNode = getObject("examples", node, false, location, result);
    if (examplesNode != null) {
      Map<String, Object> examples =
          Json.mapper()
              .convertValue(
                  examplesNode,
                  Json.mapper()
                      .getTypeFactory()
                      .constructMapType(Map.class, String.class, Object.class));
      output.setExamples(examples);
    }

    // extra keys
    Set<String> keys = getKeys(node);
    for (String key : keys) {
      if (key.startsWith("x-")) {
        output.setVendorExtension(key, extension(node.get(key)));
      } else if (!RESPONSE_KEYS.contains(key)) {
        result.extra(location, key, node.get(key));
      }
    }
    return output;
  }
  @Test
  public void testInlineMapResponse() throws Exception {
    Swagger swagger = new Swagger();

    MapProperty schema = new MapProperty();
    schema.setAdditionalProperties(new StringProperty());

    swagger.path(
        "/foo/baz",
        new Path()
            .get(
                new Operation()
                    .response(
                        200,
                        new Response()
                            .vendorExtension("x-foo", "bar")
                            .description("it works!")
                            .schema(schema))));
    new InlineModelResolver().flatten(swagger);
    Json.prettyPrint(swagger);

    Response response = swagger.getPaths().get("/foo/baz").getGet().getResponses().get("200");

    Property property = response.getSchema();
    assertTrue(property instanceof MapProperty);
    assertTrue(swagger.getDefinitions().size() == 0);
  }
Пример #4
0
  @Override
  public void processSwagger(Swagger swagger) {
    String swaggerString = Json.pretty(swagger);

    try {
      String outputFile = outputFolder + File.separator + "swagger.json";
      FileUtils.writeStringToFile(new File(outputFile), swaggerString);
      LOGGER.debug("wrote file to " + outputFile);
    } catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
    }
  }
 /**
  * Returns the API documentation of all annotated resources for purposes of Swagger documentation.
  *
  * @return The resource's documentation
  */
 @GET
 @Path("/swagger.json")
 @Produces(MediaType.APPLICATION_JSON)
 public HttpResponse getSwaggerJSON() {
   Swagger swagger = new Reader(new Swagger()).read(this.getClass());
   if (swagger == null) {
     return new HttpResponse(
         "Swagger API declaration not available!", HttpURLConnection.HTTP_NOT_FOUND);
   }
   try {
     return new HttpResponse(Json.mapper().writeValueAsString(swagger), HttpURLConnection.HTTP_OK);
   } catch (JsonProcessingException e) {
     e.printStackTrace();
     return new HttpResponse(e.getMessage(), HttpURLConnection.HTTP_INTERNAL_ERROR);
   }
 }
  @Test
  public void testBasicInput() {
    Swagger swagger = new Swagger();

    ModelImpl user = new ModelImpl().property("name", new StringProperty());

    swagger.path(
        "/foo/baz",
        new Path()
            .post(
                new Operation()
                    .parameter(new BodyParameter().name("myBody").schema(new RefModel("User")))));

    swagger.addDefinition("User", user);

    new InlineModelResolver().flatten(swagger);

    Json.prettyPrint(swagger);
  }
  /** verifies that the return value uses the schema example */
  @Test
  public void verifyGetComplexResponseWithExample() throws Exception {
    Map<String, String> queryParams = new HashMap<String, String>();

    String str =
        client.invokeAPI(
            "/mockResponses/complexResponseWithExample",
            "GET",
            queryParams,
            null,
            new HashMap<String, String>(),
            null,
            "application/json",
            null,
            new String[0]);
    ObjectMapper mapper =
        Json.mapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
    assertEquals(
        mapper.readValue(str, JsonNode.class),
        mapper.readValue("{\n  \"foo\":\"bar\"\n}\n", JsonNode.class));
  }
  /** verifies that the return value generates a schema */
  @Test
  public void verifyGetComplexResponse() throws Exception {
    Map<String, String> queryParams = new HashMap<String, String>();

    String str =
        client.invokeAPI(
            "/mockResponses/complexResponse",
            "GET",
            queryParams,
            null,
            new HashMap<String, String>(),
            null,
            "application/json",
            null,
            new String[0]);
    ObjectMapper mapper =
        Json.mapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
    assertEquals(
        mapper.readValue(str, JsonNode.class),
        mapper.readValue(
            "{\"street\":\"12345 El Monte Road\",\"city\":\"Los Altos Hills\",\"state\":\"CA\",\"zip\":\"94022\"}",
            JsonNode.class));
  }
  @Override
  public void run(InflectorServerConfiguration configuration, Environment environment)
      throws Exception {
    final FilterRegistration.Dynamic cors =
        environment.servlets().addFilter("crossOriginRequsts", CrossOriginFilter.class);
    cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");

    SwaggerInflector inflector =
        new SwaggerInflector(Configuration.read(configuration.getConfig()));
    environment.jersey().getResourceConfig().registerResources(inflector.getResources());

    // add serializers for swagger
    environment.jersey().register(SwaggerSerializers.class);

    // example serializers
    environment.jersey().register(ExampleSerializer.class);

    // mappers
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(new JsonNodeExampleSerializer());
    Json.mapper().registerModule(simpleModule);
    Yaml.mapper().registerModule(simpleModule);
  }
Пример #10
0
  private void assertDescriptorJson(Operation o, Throwable e) {
    if (e != null) {
      e.printStackTrace();

      if (e.getMessage().contains("Unparseable JSON body")) {
        // Ignore failure
        // Expecting GSON classloading issue to be fixed:
        //  - https://github.com/google/gson/issues/764
        //  - https://www.pivotaltracker.com/story/show/120885303
        Utils.logWarning("GSON initialization failure: %s", e);
        // Stop assertion logic here, test will finish as success
        return;
      } else {
        fail(e.getMessage());
      }
    }

    try {
      Swagger swagger = Json.mapper().readValue(o.getBody(String.class), Swagger.class);
      assertSwagger(swagger);
    } catch (IOException ioe) {
      fail(ioe.getMessage());
    }
  }
  public Swagger parseRoot(JsonNode node, ParseResult result) {
    String location = "";
    Swagger swagger = new Swagger();
    if (node.getNodeType().equals(JsonNodeType.OBJECT)) {
      ObjectNode on = (ObjectNode) node;
      Iterator<JsonNode> it = null;

      // required
      String value = getString("swagger", on, true, location, result);
      swagger.setSwagger(value);

      ObjectNode obj = getObject("info", on, true, "", result);
      if (obj != null) {
        Info info = info(obj, "info", result);
        swagger.info(info);
      }

      // optional
      value = getString("host", on, false, location, result);
      swagger.setHost(value);

      value = getString("basePath", on, false, location, result);
      swagger.setBasePath(value);

      ArrayNode array = getArray("schemes", on, false, location, result);
      if (array != null) {
        it = array.iterator();
        while (it.hasNext()) {
          JsonNode n = it.next();
          String s = getString(n, location + ".schemes", result);
          if (s != null) {
            Scheme scheme = Scheme.forValue(s);
            if (scheme != null) {
              swagger.scheme(scheme);
            }
          }
        }
      }

      array = getArray("consumes", on, false, location, result);
      if (array != null) {
        it = array.iterator();
        while (it.hasNext()) {
          JsonNode n = it.next();
          String s = getString(n, location + ".consumes", result);
          if (s != null) {
            swagger.consumes(s);
          }
        }
      }

      array = getArray("produces", on, false, location, result);
      if (array != null) {
        it = array.iterator();
        while (it.hasNext()) {
          JsonNode n = it.next();
          String s = getString(n, location + ".produces", result);
          if (s != null) {
            swagger.produces(s);
          }
        }
      }

      obj = getObject("paths", on, true, location, result);
      Map<String, Path> paths = paths(obj, "paths", result);
      swagger.paths(paths);

      obj = getObject("definitions", on, false, location, result);
      Map<String, Model> definitions = definitions(obj, "definitions", result);
      swagger.setDefinitions(definitions);

      obj = getObject("parameters", on, false, location, result);
      // TODO: parse
      Map<String, Parameter> parameters =
          Json.mapper()
              .convertValue(
                  obj,
                  Json.mapper()
                      .getTypeFactory()
                      .constructMapType(Map.class, String.class, Parameter.class));
      swagger.setParameters(parameters);

      obj = getObject("responses", on, false, location, result);
      Map<String, Response> responses = responses(obj, "responses", result);
      swagger.responses(responses);

      obj = getObject("securityDefinitions", on, false, location, result);
      Map<String, SecuritySchemeDefinition> securityDefinitions =
          securityDefinitions(obj, location, result);
      swagger.setSecurityDefinitions(securityDefinitions);

      array = getArray("security", on, false, location, result);
      List<SecurityRequirement> security = securityRequirements(array, location, result);
      swagger.setSecurity(security);

      array = getArray("tags", on, false, location, result);
      List<Tag> tags = tags(array, location, result);
      swagger.tags(tags);

      obj = getObject("externalDocs", on, false, location, result);
      ExternalDocs docs = externalDocs(obj, location, result);
      swagger.externalDocs(docs);

      // extra keys
      Set<String> keys = getKeys(on);
      for (String key : keys) {
        if (key.startsWith("x-")) {
          swagger.vendorExtension(key, extension(on.get(key)));
        } else if (!ROOT_KEYS.contains(key)) {
          result.extra(location, key, node.get(key));
        }
      }
    } else {
      result.invalidType("", "", "object", node);
      result.invalid();
      return null;
    }
    return swagger;
  }
  public Model definition(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
      result.missing(location, "empty schema");
    }
    if (node.get("$ref") != null) {
      return refModel(node, location, result);
    }
    if (node.get("allOf") != null) {
      return allOfModel(node, location, result);
    }
    Model model = null;
    String value = null;

    String type = getString("type", node, false, location, result);
    Model m = new ModelImpl();
    if ("array".equals(type)) {
      ArrayModel am = new ArrayModel();
      ObjectNode propertyNode = getObject("properties", node, false, location, result);
      Map<String, Property> properties = properties(propertyNode, location, result);
      am.setProperties(properties);

      ObjectNode itemsNode = getObject("items", node, false, location, result);
      Property items = property(itemsNode, location, result);
      if (items != null) {
        am.items(items);
      }

      model = am;
    } else {
      ModelImpl impl = new ModelImpl();
      impl.setType(value);

      JsonNode ap = node.get("additionalProperties");
      if (ap != null && ap.getNodeType().equals(JsonNodeType.OBJECT)) {
        impl.setAdditionalProperties(Json.mapper().convertValue(ap, Property.class));
      }

      value = getString("default", node, false, location, result);
      impl.setDefaultValue(value);

      value = getString("format", node, false, location, result);
      impl.setFormat(value);

      value = getString("discriminator", node, false, location, result);
      impl.setDiscriminator(value);

      JsonNode xml = node.get("xml");
      if (xml != null) {
        impl.setXml(Json.mapper().convertValue(xml, Xml.class));
      }

      ObjectNode externalDocs = getObject("externalDocs", node, false, location, result);
      ExternalDocs docs = externalDocs(externalDocs, location, result);
      impl.setExternalDocs(docs);

      ObjectNode properties = getObject("properties", node, true, location, result);
      if (properties != null) {
        Set<String> propertyNames = getKeys(properties);
        for (String propertyName : propertyNames) {
          JsonNode propertyNode = properties.get(propertyName);
          if (propertyNode.getNodeType().equals(JsonNodeType.OBJECT)) {
            ObjectNode on = (ObjectNode) propertyNode;
            Property property = property(on, location, result);
            impl.property(propertyName, property);
          } else {
            result.invalidType(location, "properties", "object", propertyNode);
          }
        }
      }

      // need to set properties first
      ArrayNode required = getArray("required", node, false, location, result);
      if (required != null) {
        List<String> requiredProperties = new ArrayList<String>();
        for (JsonNode n : required) {
          if (n.getNodeType().equals(JsonNodeType.STRING)) {
            requiredProperties.add(((TextNode) n).textValue());
          } else {
            result.invalidType(location, "required", "string", n);
          }
        }
        if (requiredProperties.size() > 0) {
          impl.setRequired(requiredProperties);
        }
      }

      // extra keys
      Set<String> keys = getKeys(node);
      for (String key : keys) {
        if (key.startsWith("x-")) {
          impl.setVendorExtension(key, extension(node.get(key)));
        } else if (!SCHEMA_KEYS.contains(key)) {
          result.extra(location, key, node.get(key));
        }
      }
      if ("{ }".equals(Json.pretty(impl))) return null;
      model = impl;
    }
    JsonNode exampleNode = node.get("example");
    if (exampleNode != null) {
      // we support text or object nodes
      if (exampleNode.getNodeType().equals(JsonNodeType.OBJECT)) {
        ObjectNode on = getObject("example", node, false, location, result);
        if (on != null) {
          model.setExample(on);
        }
      } else {
        model.setExample(exampleNode.asText());
      }
    }

    if (model != null) {
      value = getString("description", node, false, location, result);
      model.setDescription(value);

      value = getString("title", node, false, location, result);
      model.setTitle(value);
    }

    return model;
  }
Пример #13
0
  @Override
  public List<File> generate() {
    Boolean generateApis = null;
    Boolean generateModels = null;
    Boolean generateSupportingFiles = null;

    Set<String> modelsToGenerate = null;
    Set<String> apisToGenerate = null;
    Set<String> supportingFilesToGenerate = null;

    // allows generating only models by specifying a CSV of models to generate, or empty for all
    if (System.getProperty("models") != null) {
      String modelNames = System.getProperty("models");
      generateModels = true;
      if (!modelNames.isEmpty()) {
        modelsToGenerate = new HashSet<String>(Arrays.asList(modelNames.split(",")));
      }
    }
    if (System.getProperty("apis") != null) {
      String apiNames = System.getProperty("apis");
      generateApis = true;
      if (!apiNames.isEmpty()) {
        apisToGenerate = new HashSet<String>(Arrays.asList(apiNames.split(",")));
      }
    }
    if (System.getProperty("supportingFiles") != null) {
      String supportingFiles = System.getProperty("supportingFiles");
      generateSupportingFiles = true;
      if (!supportingFiles.isEmpty()) {
        supportingFilesToGenerate = new HashSet<String>(Arrays.asList(supportingFiles.split(",")));
      }
    }

    if (generateApis == null && generateModels == null && generateSupportingFiles == null) {
      // no specifics are set, generate everything
      generateApis = true;
      generateModels = true;
      generateSupportingFiles = true;
    } else {
      if (generateApis == null) {
        generateApis = false;
      }
      if (generateModels == null) {
        generateModels = false;
      }
      if (generateSupportingFiles == null) {
        generateSupportingFiles = false;
      }
    }

    if (swagger == null || config == null) {
      throw new RuntimeException("missing swagger input or config!");
    }
    if (System.getProperty("debugSwagger") != null) {
      Json.prettyPrint(swagger);
    }
    List<File> files = new ArrayList<File>();
    config.processOpts();
    config.preprocessSwagger(swagger);

    config.additionalProperties().put("generatedDate", DateTime.now().toString());
    config.additionalProperties().put("generatorClass", config.getClass().toString());

    if (swagger.getInfo() != null) {
      Info info = swagger.getInfo();
      if (info.getTitle() != null) {
        config.additionalProperties().put("appName", info.getTitle());
      }
      if (info.getVersion() != null) {
        config.additionalProperties().put("appVersion", info.getVersion());
      }
      if (info.getDescription() != null) {
        config
            .additionalProperties()
            .put("appDescription", config.escapeText(info.getDescription()));
      }
      if (info.getContact() != null) {
        Contact contact = info.getContact();
        config.additionalProperties().put("infoUrl", contact.getUrl());
        if (contact.getEmail() != null) {
          config.additionalProperties().put("infoEmail", contact.getEmail());
        }
      }
      if (info.getLicense() != null) {
        License license = info.getLicense();
        if (license.getName() != null) {
          config.additionalProperties().put("licenseInfo", license.getName());
        }
        if (license.getUrl() != null) {
          config.additionalProperties().put("licenseUrl", license.getUrl());
        }
      }
      if (info.getVersion() != null) {
        config.additionalProperties().put("version", info.getVersion());
      }
    }

    StringBuilder hostBuilder = new StringBuilder();
    String scheme;
    if (swagger.getSchemes() != null && swagger.getSchemes().size() > 0) {
      scheme = swagger.getSchemes().get(0).toValue();
    } else {
      scheme = "https";
    }
    hostBuilder.append(scheme);
    hostBuilder.append("://");
    if (swagger.getHost() != null) {
      hostBuilder.append(swagger.getHost());
    } else {
      hostBuilder.append("localhost");
    }
    if (swagger.getBasePath() != null) {
      hostBuilder.append(swagger.getBasePath());
    }
    String contextPath = swagger.getBasePath() == null ? "" : swagger.getBasePath();
    String basePath = hostBuilder.toString();
    String basePathWithoutHost = swagger.getBasePath();

    // resolve inline models
    InlineModelResolver inlineModelResolver = new InlineModelResolver();
    inlineModelResolver.flatten(swagger);

    List<Object> allOperations = new ArrayList<Object>();
    List<Object> allModels = new ArrayList<Object>();

    // models
    Map<String, Model> definitions = swagger.getDefinitions();
    if (definitions != null) {
      List<String> sortedModelKeys = sortModelsByInheritance(definitions);

      if (generateModels) {
        if (modelsToGenerate != null && modelsToGenerate.size() > 0) {
          List<String> updatedKeys = new ArrayList<String>();
          for (String m : sortedModelKeys) {
            if (modelsToGenerate.contains(m)) {
              updatedKeys.add(m);
            }
          }
          sortedModelKeys = updatedKeys;
        }

        for (String name : sortedModelKeys) {
          try {
            // don't generate models that have an import mapping
            if (config.importMapping().containsKey(name)) {
              continue;
            }

            Model model = definitions.get(name);
            Map<String, Model> modelMap = new HashMap<String, Model>();
            modelMap.put(name, model);
            Map<String, Object> models = processModels(config, modelMap, definitions);
            models.putAll(config.additionalProperties());

            allModels.add(((List<Object>) models.get("models")).get(0));

            for (String templateName : config.modelTemplateFiles().keySet()) {
              String suffix = config.modelTemplateFiles().get(templateName);
              String filename =
                  config.modelFileFolder() + File.separator + config.toModelFilename(name) + suffix;
              if (!config.shouldOverwrite(filename)) {
                continue;
              }
              String templateFile = getFullTemplateFile(config, templateName);
              String template = readTemplate(templateFile);
              Template tmpl =
                  Mustache.compiler()
                      .withLoader(
                          new Mustache.TemplateLoader() {
                            @Override
                            public Reader getTemplate(String name) {
                              return getTemplateReader(
                                  getFullTemplateFile(config, name + ".mustache"));
                            }
                          })
                      .defaultValue("")
                      .compile(template);
              writeToFile(filename, tmpl.execute(models));
              files.add(new File(filename));
            }
          } catch (Exception e) {
            throw new RuntimeException("Could not generate model '" + name + "'", e);
          }
        }
      }
    }
    if (System.getProperty("debugModels") != null) {
      System.out.println("############ Model info ############");
      Json.prettyPrint(allModels);
    }

    // apis
    Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
    if (generateApis) {
      if (apisToGenerate != null && apisToGenerate.size() > 0) {
        Map<String, List<CodegenOperation>> updatedPaths =
            new TreeMap<String, List<CodegenOperation>>();
        for (String m : paths.keySet()) {
          if (apisToGenerate.contains(m)) {
            updatedPaths.put(m, paths.get(m));
          }
        }
        paths = updatedPaths;
      }
      for (String tag : paths.keySet()) {
        try {
          List<CodegenOperation> ops = paths.get(tag);
          Map<String, Object> operation = processOperations(config, tag, ops);

          operation.put("basePath", basePath);
          operation.put("basePathWithoutHost", basePathWithoutHost);
          operation.put("contextPath", contextPath);
          operation.put("baseName", tag);
          operation.put("modelPackage", config.modelPackage());
          operation.putAll(config.additionalProperties());
          operation.put("classname", config.toApiName(tag));
          operation.put("classVarName", config.toApiVarName(tag));
          operation.put("importPath", config.toApiImport(tag));

          // Pass sortParamsByRequiredFlag through to the Mustache template...
          boolean sortParamsByRequiredFlag = true;
          if (this.config
              .additionalProperties()
              .containsKey(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG)) {
            sortParamsByRequiredFlag =
                Boolean.valueOf(
                    (String)
                        this.config
                            .additionalProperties()
                            .get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG)
                            .toString());
          }
          operation.put("sortParamsByRequiredFlag", sortParamsByRequiredFlag);

          processMimeTypes(swagger.getConsumes(), operation, "consumes");
          processMimeTypes(swagger.getProduces(), operation, "produces");

          allOperations.add(new HashMap<String, Object>(operation));
          for (int i = 0; i < allOperations.size(); i++) {
            Map<String, Object> oo = (Map<String, Object>) allOperations.get(i);
            if (i < (allOperations.size() - 1)) {
              oo.put("hasMore", "true");
            }
          }

          for (String templateName : config.apiTemplateFiles().keySet()) {
            String filename = config.apiFilename(templateName, tag);
            if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
              continue;
            }

            String templateFile = getFullTemplateFile(config, templateName);
            String template = readTemplate(templateFile);
            Template tmpl =
                Mustache.compiler()
                    .withLoader(
                        new Mustache.TemplateLoader() {
                          @Override
                          public Reader getTemplate(String name) {
                            return getTemplateReader(
                                getFullTemplateFile(config, name + ".mustache"));
                          }
                        })
                    .defaultValue("")
                    .compile(template);

            writeToFile(filename, tmpl.execute(operation));
            files.add(new File(filename));
          }
        } catch (Exception e) {
          throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
        }
      }
    }
    if (System.getProperty("debugOperations") != null) {
      System.out.println("############ Operation info ############");
      Json.prettyPrint(allOperations);
    }

    // supporting files
    Map<String, Object> bundle = new HashMap<String, Object>();
    bundle.putAll(config.additionalProperties());
    bundle.put("apiPackage", config.apiPackage());

    Map<String, Object> apis = new HashMap<String, Object>();
    apis.put("apis", allOperations);
    if (swagger.getHost() != null) {
      bundle.put("host", swagger.getHost());
    }
    bundle.put("basePath", basePath);
    bundle.put("scheme", scheme);
    bundle.put("contextPath", contextPath);
    bundle.put("apiInfo", apis);
    bundle.put("models", allModels);
    bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar));
    bundle.put("modelPackage", config.modelPackage());
    List<CodegenSecurity> authMethods = config.fromSecurity(swagger.getSecurityDefinitions());
    if (authMethods != null && !authMethods.isEmpty()) {
      bundle.put("authMethods", authMethods);
      bundle.put("hasAuthMethods", true);
    }
    if (swagger.getExternalDocs() != null) {
      bundle.put("externalDocs", swagger.getExternalDocs());
    }
    for (int i = 0; i < allModels.size() - 1; i++) {
      HashMap<String, CodegenModel> cm = (HashMap<String, CodegenModel>) allModels.get(i);
      CodegenModel m = cm.get("model");
      m.hasMoreModels = true;
    }

    config.postProcessSupportingFileData(bundle);

    if (System.getProperty("debugSupportingFiles") != null) {
      System.out.println("############ Supporting file info ############");
      Json.prettyPrint(bundle);
    }

    if (generateSupportingFiles) {
      for (SupportingFile support : config.supportingFiles()) {
        try {
          String outputFolder = config.outputFolder();
          if (isNotEmpty(support.folder)) {
            outputFolder += File.separator + support.folder;
          }
          File of = new File(outputFolder);
          if (!of.isDirectory()) {
            of.mkdirs();
          }
          String outputFilename = outputFolder + File.separator + support.destinationFilename;
          if (!config.shouldOverwrite(outputFilename)) {
            continue;
          }

          String templateFile = getFullTemplateFile(config, support.templateFile);

          boolean shouldGenerate = true;
          if (supportingFilesToGenerate != null && supportingFilesToGenerate.size() > 0) {
            if (supportingFilesToGenerate.contains(support.destinationFilename)) {
              shouldGenerate = true;
            } else {
              shouldGenerate = false;
            }
          }
          if (shouldGenerate) {
            if (templateFile.endsWith("mustache")) {
              String template = readTemplate(templateFile);
              Template tmpl =
                  Mustache.compiler()
                      .withLoader(
                          new Mustache.TemplateLoader() {
                            @Override
                            public Reader getTemplate(String name) {
                              return getTemplateReader(
                                  getFullTemplateFile(config, name + ".mustache"));
                            }
                          })
                      .defaultValue("")
                      .compile(template);

              writeToFile(outputFilename, tmpl.execute(bundle));
              files.add(new File(outputFilename));
            } else {
              InputStream in = null;

              try {
                in = new FileInputStream(templateFile);
              } catch (Exception e) {
                // continue
              }
              if (in == null) {
                in =
                    this.getClass()
                        .getClassLoader()
                        .getResourceAsStream(getCPResourcePath(templateFile));
              }
              File outputFile = new File(outputFilename);
              OutputStream out = new FileOutputStream(outputFile, false);
              if (in != null && out != null) {
                System.out.println("writing file " + outputFile);
                IOUtils.copy(in, out);
              } else {
                if (in == null) {
                  System.out.println("can't open " + templateFile + " for input");
                }
                if (out == null) {
                  System.out.println("can't open " + outputFile + " for output");
                }
              }

              files.add(outputFile);
            }
          }
        } catch (Exception e) {
          throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
        }
      }
    }

    config.processSwagger(swagger);

    return files;
  }
  public Parameter parameter(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
      return null;
    }

    Parameter output = null;
    JsonNode ref = obj.get("$ref");
    if (ref != null) {
      if (ref.getNodeType().equals(JsonNodeType.STRING)) {
        return refParameter((TextNode) ref, location, result);
      } else {
        result.invalidType(location, "$ref", "string", obj);
        return null;
      }
    }

    String l = null;
    JsonNode ln = obj.get("name");
    if (ln != null) {
      l = ln.asText();
    } else {
      l = "['unknown']";
    }
    location += ".[" + l + "]";

    String value = getString("in", obj, true, location, result);
    if (value != null) {
      String type = getString("type", obj, false, location, result);
      String format = getString("format", obj, false, location, result);
      AbstractSerializableParameter<?> sp = null;
      if ("query".equals(value)) {
        sp = new QueryParameter();
      } else if ("header".equals(value)) {
        sp = new HeaderParameter();
      } else if ("path".equals(value)) {
        sp = new PathParameter();
      } else if ("formData".equals(value)) {
        sp = new FormParameter();
      }

      if (sp != null) {
        // type is mandatory when sp != null
        getString("type", obj, true, location, result);
        Map<PropertyBuilder.PropertyId, Object> map =
            new HashMap<PropertyBuilder.PropertyId, Object>();

        map.put(TYPE, type);
        map.put(FORMAT, format);
        String defaultValue = getString("default", obj, false, location, result);
        map.put(DEFAULT, defaultValue);
        sp.setDefault(defaultValue);

        Double dbl = getDouble("maximum", obj, false, location, result);
        if (dbl != null) {
          map.put(MAXIMUM, dbl);
          sp.setMaximum(dbl);
        }

        Boolean bl = getBoolean("exclusiveMaximum", obj, false, location, result);
        if (bl != null) {
          map.put(EXCLUSIVE_MAXIMUM, bl);
          sp.setExclusiveMaximum(bl);
        }

        dbl = getDouble("minimum", obj, false, location, result);
        if (dbl != null) {
          map.put(MINIMUM, dbl);
          sp.setMinimum(dbl);
        }

        bl = getBoolean("exclusiveMinimum", obj, false, location, result);
        if (bl != null) {
          map.put(EXCLUSIVE_MINIMUM, bl);
          sp.setExclusiveMinimum(bl);
        }

        map.put(MAX_LENGTH, getInteger("maxLength", obj, false, location, result));
        map.put(MIN_LENGTH, getInteger("minLength", obj, false, location, result));

        String pat = getString("pattern", obj, false, location, result);
        map.put(PATTERN, pat);
        sp.setPattern(pat);

        Integer iv = getInteger("maxItems", obj, false, location, result);
        map.put(MAX_ITEMS, iv);
        sp.setMaxItems(iv);

        iv = getInteger("minItems", obj, false, location, result);
        map.put(MIN_ITEMS, iv);
        sp.setMinItems(iv);

        map.put(UNIQUE_ITEMS, getBoolean("uniqueItems", obj, false, location, result));

        ArrayNode an = getArray("enum", obj, false, location, result);
        if (an != null) {
          List<String> _enum = new ArrayList<String>();
          for (JsonNode n : an) {
            _enum.add(n.textValue());
          }
          sp.setEnum(_enum);
          map.put(ENUM, _enum);
        }

        Property prop = PropertyBuilder.build(type, format, map);

        if (prop != null) {
          sp.setProperty(prop);
          ObjectNode items = getObject("items", obj, false, location, result);
          if (items != null) {
            Property inner = schema(null, items, location, result);
            sp.setItems(inner);
          }
        }

        Set<String> keys = getKeys(obj);
        for (String key : keys) {
          if (key.startsWith("x-")) {
            sp.setVendorExtension(key, extension(obj.get(key)));
          } else if (!PARAMETER_KEYS.contains(key)) {
            result.extra(location, key, obj.get(key));
          }
        }

        String collectionFormat = getString("collectionFormat", obj, false, location, result);
        sp.setCollectionFormat(collectionFormat);

        output = sp;
      } else if ("body".equals(value)) {
        output = Json.mapper().convertValue(obj, Parameter.class);
      }
      if (output != null) {
        value = getString("name", obj, true, location, result);
        output.setName(value);

        value = getString("description", obj, false, location, result);
        output.setDescription(value);

        Boolean required = getBoolean("required", obj, false, location, result);
        if (required != null) {
          output.setRequired(required);
        }
      }
    }

    return output;
  }
 private Property schema(
     Map<String, Object> schemaItems, JsonNode obj, String location, ParseResult result) {
   return Json.mapper().convertValue(obj, Property.class);
 }
 public static boolean compareAsJson(Object objectToSerialize, String jsonStr) {
   return apply(objectToSerialize, jsonStr, Json.mapper());
 }
 public Property property(ObjectNode node, String location, ParseResult result) {
   Property output = Json.mapper().convertValue(node, Property.class);
   return output;
 }