|
47 | 47 | import java.util.stream.Collectors;
|
48 | 48 | import java.util.stream.Stream;
|
49 | 49 |
|
50 |
| -import com.fasterxml.jackson.core.JsonProcessingException; |
51 | 50 | import com.fasterxml.jackson.databind.ObjectMapper;
|
52 | 51 | import io.swagger.v3.core.jackson.TypeNameResolver;
|
53 | 52 | import io.swagger.v3.core.util.AnnotationsUtils;
|
|
98 | 97 | import static org.springdoc.core.utils.Constants.DEFAULT_SERVER_DESCRIPTION;
|
99 | 98 | import static org.springdoc.core.utils.Constants.DEFAULT_TITLE;
|
100 | 99 | import static org.springdoc.core.utils.Constants.DEFAULT_VERSION;
|
| 100 | +import static org.springdoc.core.utils.SpringDocUtils.cloneViaJson; |
101 | 101 | import static org.springdoc.core.utils.SpringDocUtils.getConfig;
|
102 | 102 |
|
103 | 103 | /**
|
@@ -246,14 +246,7 @@ public OpenAPI build(Locale locale) {
|
246 | 246 | calculatedOpenAPI.setPaths(new Paths());
|
247 | 247 | }
|
248 | 248 | else {
|
249 |
| - try { |
250 |
| - ObjectMapper objectMapper = new ObjectMapper(); |
251 |
| - calculatedOpenAPI = objectMapper.readValue(objectMapper.writeValueAsString(openAPI), OpenAPI.class); |
252 |
| - } |
253 |
| - catch (JsonProcessingException e) { |
254 |
| - LOGGER.warn("Json Processing Exception occurred: {}", e.getMessage()); |
255 |
| - calculatedOpenAPI = openAPI; |
256 |
| - } |
| 249 | + calculatedOpenAPI = cloneViaJson(openAPI, OpenAPI.class, new ObjectMapper()); |
257 | 250 | }
|
258 | 251 |
|
259 | 252 | if (apiDef.isPresent()) {
|
@@ -539,71 +532,71 @@ private Optional<OpenAPIDefinition> getOpenAPIDefinition() {
|
539 | 532 | }
|
540 | 533 |
|
541 | 534 |
|
542 |
| - /** |
543 |
| - * Gets webhooks from given classes. |
544 |
| - * |
545 |
| - * @param classes Array of classes to scan for webhooks. |
546 |
| - * @return An array of {@link Webhooks} annotations found in the given classes. |
547 |
| - */ |
548 |
| - public Webhooks[] getWebhooks(Class<?>[] classes) { |
549 |
| - List<Webhooks> allWebhooks = new ArrayList<>(); |
550 |
| - |
551 |
| - for (Class<?> clazz : classes) { |
552 |
| - // Class-level annotations |
553 |
| - collectWebhooksFromElement(clazz, allWebhooks); |
554 |
| - |
555 |
| - // Method-level annotations |
556 |
| - for (Method method : clazz.getDeclaredMethods()) { |
557 |
| - collectWebhooksFromElement(method, allWebhooks); |
558 |
| - } |
559 |
| - } |
560 |
| - |
561 |
| - return allWebhooks.toArray(new Webhooks[0]); |
562 |
| - } |
563 |
| - |
564 |
| - |
565 |
| - /** |
566 |
| - * Retrieves all classes related to webhooks. |
567 |
| - * This method scans for classes annotated with {@link Webhooks} or {@link Webhook}, |
568 |
| - * first checking Spring-managed beans and then falling back to classpath scanning |
569 |
| - * if no annotated beans are found. |
570 |
| - * |
571 |
| - * @return An array of classes related to webhooks. |
572 |
| - */ |
573 |
| - public Class<?>[] getWebhooksClasses() { |
574 |
| - Set<Class<?>> allWebhookClassesToScan = new HashSet<>(); |
575 |
| - |
576 |
| - // First: scan Spring-managed beans |
577 |
| - Map<String, Object> beans = context.getBeansWithAnnotation(Webhooks.class); |
578 |
| - |
579 |
| - for (Object bean : beans.values()) { |
580 |
| - Class<?> beanClass = bean.getClass(); |
581 |
| - allWebhookClassesToScan.add(beanClass); |
582 |
| - } |
583 |
| - |
584 |
| - // Fallback: classpath scanning |
585 |
| - ClassPathScanningCandidateComponentProvider scanner = |
586 |
| - new ClassPathScanningCandidateComponentProvider(false); |
587 |
| - scanner.addIncludeFilter(new AnnotationTypeFilter(Webhooks.class)); |
588 |
| - scanner.addIncludeFilter(new AnnotationTypeFilter(Webhook.class)); |
589 |
| - |
590 |
| - if (AutoConfigurationPackages.has(context)) { |
591 |
| - for (String basePackage : AutoConfigurationPackages.get(context)) { |
592 |
| - Set<BeanDefinition> candidates = scanner.findCandidateComponents(basePackage); |
593 |
| - for (BeanDefinition bd : candidates) { |
594 |
| - try { |
595 |
| - Class<?> clazz = Class.forName(bd.getBeanClassName()); |
596 |
| - allWebhookClassesToScan.add(clazz); |
597 |
| - } |
598 |
| - catch (ClassNotFoundException e) { |
599 |
| - LOGGER.error("Class not found in classpath: {}", e.getMessage()); |
600 |
| - } |
601 |
| - } |
602 |
| - } |
603 |
| - } |
604 |
| - |
605 |
| - return allWebhookClassesToScan.toArray(new Class<?>[0]); |
606 |
| - } |
| 535 | + /** |
| 536 | + * Gets webhooks from given classes. |
| 537 | + * |
| 538 | + * @param classes Array of classes to scan for webhooks. |
| 539 | + * @return An array of {@link Webhooks} annotations found in the given classes. |
| 540 | + */ |
| 541 | + public Webhooks[] getWebhooks(Class<?>[] classes) { |
| 542 | + List<Webhooks> allWebhooks = new ArrayList<>(); |
| 543 | + |
| 544 | + for (Class<?> clazz : classes) { |
| 545 | + // Class-level annotations |
| 546 | + collectWebhooksFromElement(clazz, allWebhooks); |
| 547 | + |
| 548 | + // Method-level annotations |
| 549 | + for (Method method : clazz.getDeclaredMethods()) { |
| 550 | + collectWebhooksFromElement(method, allWebhooks); |
| 551 | + } |
| 552 | + } |
| 553 | + |
| 554 | + return allWebhooks.toArray(new Webhooks[0]); |
| 555 | + } |
| 556 | + |
| 557 | + |
| 558 | + /** |
| 559 | + * Retrieves all classes related to webhooks. |
| 560 | + * This method scans for classes annotated with {@link Webhooks} or {@link Webhook}, |
| 561 | + * first checking Spring-managed beans and then falling back to classpath scanning |
| 562 | + * if no annotated beans are found. |
| 563 | + * |
| 564 | + * @return An array of classes related to webhooks. |
| 565 | + */ |
| 566 | + public Class<?>[] getWebhooksClasses() { |
| 567 | + Set<Class<?>> allWebhookClassesToScan = new HashSet<>(); |
| 568 | + |
| 569 | + // First: scan Spring-managed beans |
| 570 | + Map<String, Object> beans = context.getBeansWithAnnotation(Webhooks.class); |
| 571 | + |
| 572 | + for (Object bean : beans.values()) { |
| 573 | + Class<?> beanClass = bean.getClass(); |
| 574 | + allWebhookClassesToScan.add(beanClass); |
| 575 | + } |
| 576 | + |
| 577 | + // Fallback: classpath scanning |
| 578 | + ClassPathScanningCandidateComponentProvider scanner = |
| 579 | + new ClassPathScanningCandidateComponentProvider(false); |
| 580 | + scanner.addIncludeFilter(new AnnotationTypeFilter(Webhooks.class)); |
| 581 | + scanner.addIncludeFilter(new AnnotationTypeFilter(Webhook.class)); |
| 582 | + |
| 583 | + if (AutoConfigurationPackages.has(context)) { |
| 584 | + for (String basePackage : AutoConfigurationPackages.get(context)) { |
| 585 | + Set<BeanDefinition> candidates = scanner.findCandidateComponents(basePackage); |
| 586 | + for (BeanDefinition bd : candidates) { |
| 587 | + try { |
| 588 | + Class<?> clazz = Class.forName(bd.getBeanClassName()); |
| 589 | + allWebhookClassesToScan.add(clazz); |
| 590 | + } |
| 591 | + catch (ClassNotFoundException e) { |
| 592 | + LOGGER.error("Class not found in classpath: {}", e.getMessage()); |
| 593 | + } |
| 594 | + } |
| 595 | + } |
| 596 | + } |
| 597 | + |
| 598 | + return allWebhookClassesToScan.toArray(new Class<?>[0]); |
| 599 | + } |
607 | 600 |
|
608 | 601 |
|
609 | 602 | /**
|
@@ -716,7 +709,7 @@ private Info resolveProperties(Info info, Map<String, Object> extensions, Locale
|
716 | 709 |
|
717 | 710 | if (extensions != null) {
|
718 | 711 | Map<String, Object> extensionsResolved = propertyResolverUtils.resolveExtensions(locale, extensions);
|
719 |
| - if (propertyResolverUtils.isOpenapi31()){ |
| 712 | + if (propertyResolverUtils.isOpenapi31()) { |
720 | 713 | extensionsResolved.forEach(info::addExtension31);
|
721 | 714 | info.setExtensions(extensionsResolved);
|
722 | 715 | }
|
|
0 commit comments