io.swagger.v3.oas.annotations.OpenAPIDefinition Java Examples

The following examples show how to use io.swagger.v3.oas.annotations.OpenAPIDefinition. 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: AnnotationProcessorTest.java    From servicecomb-toolkit with Apache License 2.0 6 votes vote down vote up
@Test
  public void processOpenApiDefinitionClassAnnotation() throws NoSuchMethodException {

    OasContext oasContext = null;

    OpenApiDefinitionClassAnnotationProcessor openApiDefinitionClassAnnotationProcessor = new OpenApiDefinitionClassAnnotationProcessor();

    OpenAPIDefinition openAPIDefinition = OpenapiDef.class.getAnnotation(OpenAPIDefinition.class);
    oasContext = new OasContext(new AbstractAnnotationParser() {
      @Override
      public int getOrder() {
        return 0;
      }

      @Override
      public boolean canProcess(Class<?> cls) {
        return true;
      }
    });
    openApiDefinitionClassAnnotationProcessor.process(openAPIDefinition, oasContext);
//    Assert.assertEquals("hello-operation", context.getOperationId());
  }
 
Example #2
Source File: OpenAPIBuilder.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Build open api with open api definition.
 *
 * @param openAPI the open api
 * @param apiDef the api def
 */
private void buildOpenAPIWithOpenAPIDefinition(OpenAPI openAPI, OpenAPIDefinition apiDef) {
	// info
	AnnotationsUtils.getInfo(apiDef.info()).map(this::resolveProperties).ifPresent(openAPI::setInfo);
	// OpenApiDefinition security requirements
	securityParser.getSecurityRequirements(apiDef.security()).ifPresent(openAPI::setSecurity);
	// OpenApiDefinition external docs
	AnnotationsUtils.getExternalDocumentation(apiDef.externalDocs()).ifPresent(openAPI::setExternalDocs);
	// OpenApiDefinition tags
	AnnotationsUtils.getTags(apiDef.tags(), false).ifPresent(tags -> openAPI.setTags(new ArrayList<>(tags)));
	// OpenApiDefinition servers
	Optional<List<Server>> optionalServers = AnnotationsUtils.getServers(apiDef.servers());
	if (optionalServers.isPresent()) {
		openAPI.setServers(optionalServers.get());
		this.isServersPresent = true;
	}
	// OpenApiDefinition extensions
	if (apiDef.extensions().length > 0) {
		openAPI.setExtensions(AnnotationsUtils.getExtensions(apiDef.extensions()));
	}
}
 
Example #3
Source File: OpenAPIBuilder.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Gets api def class.
 *
 * @param scanner the scanner
 * @param packagesToScan the packages to scan
 * @return the api def class
 */
private OpenAPIDefinition getApiDefClass(ClassPathScanningCandidateComponentProvider scanner,
		List<String> packagesToScan) {
	for (String pack : packagesToScan) {
		for (BeanDefinition bd : scanner.findCandidateComponents(pack)) {
			// first one found is ok
			try {
				return AnnotationUtils.findAnnotation(Class.forName(bd.getBeanClassName()),
						OpenAPIDefinition.class);
			}
			catch (ClassNotFoundException e) {
				LOGGER.error("Class Not Found in classpath : {}", e.getMessage());
			}
		}
	}
	return null;
}
 
Example #4
Source File: OpenAPIBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Build.
 */
public void build() {
	Optional<OpenAPIDefinition> apiDef = getOpenAPIDefinition();

	if (openAPI == null) {
		this.calculatedOpenAPI = new OpenAPI();
		this.calculatedOpenAPI.setComponents(new Components());
		this.calculatedOpenAPI.setPaths(new Paths());
	}
	else
		this.calculatedOpenAPI = openAPI;

	if (apiDef.isPresent()) {
		buildOpenAPIWithOpenAPIDefinition(calculatedOpenAPI, apiDef.get());
	}
	// Set default info
	else if (calculatedOpenAPI.getInfo() == null) {
		Info infos = new Info().title(DEFAULT_TITLE).version(DEFAULT_VERSION);
		calculatedOpenAPI.setInfo(infos);
	}
	// Set default mappings
	this.mappingsMap.putAll(context.getBeansWithAnnotation(RestController.class));
	this.mappingsMap.putAll(context.getBeansWithAnnotation(RequestMapping.class));
	this.mappingsMap.putAll(context.getBeansWithAnnotation(Controller.class));

	// add security schemes
	this.calculateSecuritySchemes(calculatedOpenAPI.getComponents());
	openApiBuilderCustomisers.ifPresent(customisers -> customisers.forEach(customiser -> customiser.customise(this)));
}
 
Example #5
Source File: OpenAPIBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Gets open api definition.
 *
 * @return the open api definition
 */
private Optional<OpenAPIDefinition> getOpenAPIDefinition() {
	// Look for OpenAPIDefinition in a spring managed bean
	Map<String, Object> openAPIDefinitionMap = context.getBeansWithAnnotation(OpenAPIDefinition.class);
	OpenAPIDefinition apiDef = null;
	if (openAPIDefinitionMap.size() > 1)
		LOGGER.warn(
				"found more than one OpenAPIDefinition class. springdoc-openapi will be using the first one found.");
	if (openAPIDefinitionMap.size() > 0) {
		Map.Entry<String, Object> entry = openAPIDefinitionMap.entrySet().iterator().next();
		Class<?> objClz = entry.getValue().getClass();
		apiDef = AnnotatedElementUtils.findMergedAnnotation(objClz, OpenAPIDefinition.class);
	}

	// Look for OpenAPIDefinition in the spring classpath
	else {
		ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
				false);
		scanner.addIncludeFilter(new AnnotationTypeFilter(OpenAPIDefinition.class));
		if (AutoConfigurationPackages.has(context)) {
			List<String> packagesToScan = AutoConfigurationPackages.get(context);
			apiDef = getApiDefClass(scanner, packagesToScan);
		}

	}
	return Optional.ofNullable(apiDef);
}
 
Example #6
Source File: JaxRSScanner.java    From swagger-maven-plugin with MIT License 5 votes vote down vote up
Set<Class<?>> classes() {
    ConfigurationBuilder config = ConfigurationBuilder
            .build(resourcePackages)
            .setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner());
    Reflections reflections = new Reflections(config);
    Stream<Class<?>> apiClasses = reflections.getTypesAnnotatedWith(Path.class)
            .stream()
            .filter(this::filterClassByResourcePackages);
    Stream<Class<?>> defClasses = reflections.getTypesAnnotatedWith(OpenAPIDefinition.class)
            .stream()
            .filter(this::filterClassByResourcePackages);
    return Stream.concat(apiClasses, defClasses).collect(Collectors.toSet());
}
 
Example #7
Source File: AbstractAnnotationParser.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
public void initClassAnnotationProcessor() {
  classAnnotationMap.put(OpenAPIDefinition.class, new OpenApiDefinitionClassAnnotationProcessor());
}