Java Code Examples for io.swagger.models.Swagger#setDefinitions()

The following examples show how to use io.swagger.models.Swagger#setDefinitions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SwaggerHandlerImplTest.java    From spring-cloud-huawei with Apache License 2.0 6 votes vote down vote up
private void init() {
  mockMap.put("xxxx", apiListing);
  mockDoc = new Documentation("xx", "/xx", null, mockMap,
      null, Collections.emptySet(), Collections.emptySet(), null, Collections.emptySet(), Collections.emptyList());
  mockSwagger = new Swagger();
  mockSwagger.setInfo(new Info());
  Map<String, Model> defMap = new HashMap<>();
  defMap.put("xx", new ModelImpl());
  mockSwagger.setDefinitions(defMap);
  Map<String, Path> pathMap = new HashMap<>();
  pathMap.put("xx", new Path());
  mockSwagger.setPaths(pathMap);
  new Expectations() {
    {
      documentationCache.documentationByGroup(anyString);
      result = mockDoc;

      DefinitionCache.getClassNameBySchema(anyString);
      result = "app";

      mapper.mapDocumentation((Documentation) any);
      result = mockSwagger;
    }
  };
}
 
Example 2
Source File: SwaggerService.java    From heimdall with Apache License 2.0 6 votes vote down vote up
public Swagger exportApiToSwaggerJSON(Api api) {
    Swagger swagger = new Swagger();

    swagger.setSwagger("2.0");
    swagger.setBasePath(api.getBasePath());

    swagger.setInfo(getInfoByApi(api));
    Optional<Environment> optionalEnvironment = api.getEnvironments().stream().findFirst();
    optionalEnvironment.ifPresent(environment -> swagger.setHost(environment.getInboundURL()));
    swagger.setTags(getTagsByApi(api));
    swagger.setPaths(getPathsByApi(api));
    swagger.setDefinitions(new HashMap<>());
    swagger.setConsumes(new ArrayList<>());

    return swagger;
}
 
Example 3
Source File: DefinitionGeneratorTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void test_2() throws Exception
{
    Swagger swagger = new Swagger();
    swagger.setDefinitions(new HashMap<>());
    Set<String> exampleClassNames = new HashSet<>();
    Set<Class<?>> modelClasses = new HashSet<>(Arrays.asList(NonParametrizedListCase.class));
    try
    {
        new DefinitionGenerator(LOG, swagger, exampleClassNames, modelClasses, null);
        fail();
    }
    catch (Exception e)
    {
        assertEquals(MojoExecutionException.class, e.getClass());
        assertEquals("Field \"list\" is a collection that is not a ParameterizedType.", e.getMessage());
    }
}
 
Example 4
Source File: DefinitionGeneratorTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void test_3() throws Exception
{
    Swagger swagger = new Swagger();
    swagger.setDefinitions(new HashMap<>());
    Set<String> exampleClassNames = new HashSet<>();
    Set<Class<?>> modelClasses = new HashSet<>(Arrays.asList(WildcardParameterizedListCase.class));
    try
    {
        new DefinitionGenerator(LOG, swagger, exampleClassNames, modelClasses, null);
        fail();
    }
    catch (Exception e)
    {
        assertEquals(MojoExecutionException.class, e.getClass());
        assertEquals("Field \"list\" has a parameterized type which is not a class.", e.getMessage());
    }
}
 
Example 5
Source File: DefinitionGeneratorTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void test_4() throws Exception
{
    Swagger swagger = new Swagger();
    swagger.setDefinitions(new HashMap<>());
    Set<String> exampleClassNames = new HashSet<>();
    Set<Class<?>> modelClasses = new HashSet<>(Arrays.asList(MultiTypedListCase.class));
    try
    {
        new DefinitionGenerator(LOG, swagger, exampleClassNames, modelClasses, null);
        fail();
    }
    catch (Exception e)
    {
        assertEquals(MojoExecutionException.class, e.getClass());
        assertEquals("Field \"list\" is a collection that has 2 parameterized types and exactly 1 is required.", e.getMessage());
    }
}
 
Example 6
Source File: OpenAPIV2Generator.java    From spring-openapi with MIT License 5 votes vote down vote up
public Swagger generate(OpenApiV2GeneratorConfig config) {
	logger.info("Starting OpenAPI v2 generation");
	environment = config.getEnvironment();
	Swagger openAPI = new Swagger();
	openAPI.setDefinitions(createDefinitions());
	openAPI.setPaths(createPaths(config));
	openAPI.setInfo(info);
	openAPI.setBasePath(config.getBasePath());
	openAPI.setHost(config.getHost());
	logger.info("OpenAPI v2 generation done!");
	return openAPI;
}
 
Example 7
Source File: AnnotationUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public static void appendDefinition(Swagger swagger, Map<String, Model> newDefinitions) {
  if (newDefinitions.isEmpty()) {
    return;
  }

  Map<String, Model> definitions = swagger.getDefinitions();
  if (definitions == null) {
    definitions = new LinkedHashMap<>();
    swagger.setDefinitions(definitions);
  }

  definitions.putAll(newDefinitions);
}
 
Example 8
Source File: SwaggerGenerator.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
private Map<String, Model> getOrCreateDefinitionMap(Swagger swagger) {
  Map<String, Model> definitions = swagger.getDefinitions();
  if (definitions == null) {
    definitions = new LinkedHashMap<>();
    swagger.setDefinitions(definitions);
  }
  return definitions;
}
 
Example 9
Source File: DefinitionGeneratorTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void test_1() throws Exception
{
    Swagger swagger = new Swagger();
    swagger.setDefinitions(new HashMap<>());
    Set<String> exampleClassNames = new HashSet<>(Arrays.asList("BasicCase"));
    Set<Class<?>> modelClasses = new LinkedHashSet<>(Arrays.asList(BasicCase.class, TrivialCase.class, NonXmlType.class));
    new DefinitionGenerator(LOG, swagger, exampleClassNames, modelClasses, null);

    assertEquals(getResourceAsString("/yaml1.yaml"), toYaml(swagger));
}
 
Example 10
Source File: DefinitionGeneratorTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void test_5() throws Exception
{
    Swagger swagger = new Swagger();
    swagger.setDefinitions(new HashMap<>());
    Set<String> exampleClassNames = new HashSet<>();
    Set<Class<?>> modelClasses = new HashSet<>(Arrays.asList(XsdMappingCase.class));
    new DefinitionGenerator(LOG, swagger, exampleClassNames, modelClasses, new XsdParser("xsd2.xsd"));
    Model definition = swagger.getDefinitions().get("xsdMappingCase");
    assertEquals("The xsdMappingCase", definition.getDescription());
    assertEquals("The scalar1", definition.getProperties().get("scalar1").getDescription());
    assertEquals("The scalar2", definition.getProperties().get("scalar2").getDescription());
}
 
Example 11
Source File: Utils.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
public static void sortSwagger(Swagger swagger) throws GenerateException {
    if (swagger == null || swagger.getPaths() == null) {
        return;
    }

    TreeMap<String, Path> sortedMap = new TreeMap<String, Path>();
    if (swagger.getPaths() == null) {
        return;
    }
    sortedMap.putAll(swagger.getPaths());
    swagger.paths(sortedMap);

    for (Path path : swagger.getPaths().values()) {
        String methods[] = {"Get", "Delete", "Post", "Put", "Options", "Patch"};
        for (String m : methods) {
            sortResponses(path, m);
        }
    }

    //reorder definitions
    if (swagger.getDefinitions() != null) {
        TreeMap<String, Model> defs = new TreeMap<String, Model>();
        defs.putAll(swagger.getDefinitions());
        swagger.setDefinitions(defs);
    }

    // order the tags
    if (swagger.getTags() != null) {
        Collections.sort(swagger.getTags(), new Comparator<Tag>() {
            public int compare(final Tag a, final Tag b) {
                return a.toString().toLowerCase().compareTo(b.toString().toLowerCase());
            }
        });
    }

}
 
Example 12
Source File: SwaggerGenMojo.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Gets a new Swagger metadata.
 *
 * @return the Swagger metadata.
 * @throws MojoExecutionException if any problems were encountered.
 */
private Swagger getSwagger() throws MojoExecutionException
{
    getLog().debug("Creating Swagger Metadata");
    // Set up initial Swagger metadata.
    Swagger swagger = new Swagger();
    swagger.setInfo(new Info().title(title).version(version));
    swagger.setBasePath(basePath);

    // Set the schemes.
    if (!CollectionUtils.isEmpty(schemeParameters))
    {
        List<Scheme> schemes = new ArrayList<>();
        for (String schemeParameter : schemeParameters)
        {
            Scheme scheme = Scheme.forValue(schemeParameter);
            if (scheme == null)
            {
                throw new MojoExecutionException("Invalid scheme specified: " + schemeParameter);
            }
            schemes.add(scheme);
        }
        swagger.setSchemes(schemes);
    }

    // Add authorization support if specified.
    if (authType != null)
    {
        // Find the definition for the user specified type.
        SecuritySchemeDefinition securitySchemeDefinition = null;
        for (SecuritySchemeDefinition possibleDefinition : SECURITY_SCHEME_DEFINITIONS)
        {
            if (possibleDefinition.getType().equalsIgnoreCase(authType))
            {
                securitySchemeDefinition = possibleDefinition;
                break;
            }
        }

        // If we found a match, set it on the swagger object.
        if (securitySchemeDefinition != null)
        {
            // Come up with an authentication name for easy identification (e.g. basicAuthentication, etc.).
            String securityName = securitySchemeDefinition.getType() + "Authentication";

            // Add the security definition.
            swagger.addSecurityDefinition(securityName, securitySchemeDefinition);

            // Add the security for everything based on the name of the definition.
            SecurityRequirement securityRequirement = new SecurityRequirement();
            securityRequirement.requirement(securityName);
            swagger.addSecurity(securityRequirement);
        }
    }

    // Use default paths and definitions.
    swagger.setPaths(new TreeMap<>());
    swagger.setDefinitions(new TreeMap<>());
    return swagger;
}
 
Example 13
Source File: SwaggerGenerator.java    From yang2swagger with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Run Swagger generation for configured modules. Write result to target. The file format
 * depends on configured {@link SwaggerGenerator.Format}
 * @param target writer
 * @throws IOException when problem with writing
 */
public void generate(Writer target) throws IOException {
    if(target == null) throw new NullPointerException();

    Swagger result = generate();

    new SortComplexModels().accept(result);

    result.setDefinitions(SwaggerUtils.sortMap(result.getDefinitions()));
    result.setPaths(SwaggerUtils.sortMap(result.getPaths()));

    mapper.writeValue(target, result);
}