Skip to content

Fix ClassCastException in MagicNumberDiagnostic with soft error handling #3486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
337d432
Initial plan for issue
Copilot Jun 14, 2025
80a646b
Fix ClassCastException in MagicNumberDiagnostic with soft error handling
Copilot Jun 14, 2025
50e7a28
Implement general solution for ClassCastException in diagnostic confi…
Copilot Jun 14, 2025
240c0fe
Simplify ClassCastException handling to just try/catch with error log…
Copilot Jun 14, 2025
503081e
Implement localized error messages and crash test for DiagnosticBeanP…
Copilot Jun 14, 2025
d60200f
Address code review feedback: change log level, fix localization, imp…
Copilot Jun 14, 2025
ad3410a
Use getBean for DiagnosticBeanPostProcessor in test instead of manual…
Copilot Jun 14, 2025
1681c41
Add reflection-based verification of diagnostic parameter values afte…
Copilot Jun 14, 2025
d21e17d
Use DiagnosticObjectProvider and autowired DiagnosticBeanPostProcesso…
Copilot Jun 14, 2025
ea69b6e
Replace reflection with getter method for DiagnosticsOptions configur…
Copilot Jun 15, 2025
7346b69
Add verification of default authorized numbers in test as requested
Copilot Jun 15, 2025
df49d65
Implement JSON schema validation for diagnostic parameters
Copilot Jun 15, 2025
dae5993
Implement cached JSON schema validation for diagnostic parameters per…
Copilot Jun 15, 2025
0443adb
Extract DiagnosticParameterValidator class to enable Spring caching f…
Copilot Jun 15, 2025
7bdfb53
Add Javadoc to DiagnosticParameterValidator class and use @Getter(laz…
Copilot Jun 24, 2025
dafc798
Fix deprecated method usage and Spring Bean warnings in DiagnosticBea…
Copilot Jun 24, 2025
7a4c86d
Add ROLE_INFRASTRUCTURE to core infrastructure beans to resolve Sprin…
Copilot Jun 24, 2025
d69d92d
Complete Spring Bean warnings fix by adding ROLE_INFRASTRUCTURE to al…
Copilot Jun 24, 2025
7276b9d
Remove inappropriate ROLE_INFRASTRUCTURE annotations from regular bea…
Copilot Jun 24, 2025
6e38274
Fix failing tests and eliminate Spring Bean warnings by removing sche…
Copilot Jun 24, 2025
631c220
Reintegrate JSON schema validation in DiagnosticBeanPostProcessor wit…
Copilot Jun 24, 2025
291d79b
Restore DiagnosticParameterValidator component with infrastructure ro…
Copilot Jun 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import com.github._1c_syntax.bsl.languageserver.diagnostics.BSLDiagnostic;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo;
import com.github._1c_syntax.bsl.languageserver.utils.Resources;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
Expand All @@ -33,10 +35,12 @@

@RequiredArgsConstructor
@Component
@Slf4j
public class DiagnosticBeanPostProcessor implements BeanPostProcessor {

private final LanguageServerConfiguration configuration;
private final Map<Class<? extends BSLDiagnostic>, DiagnosticInfo> diagnosticInfos;
private final Resources resources;

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
Expand Down Expand Up @@ -65,7 +69,13 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
configuration.getDiagnosticsOptions().getParameters().get(diagnostic.getInfo().getCode().getStringValue());

if (diagnosticConfiguration != null && diagnosticConfiguration.isRight()) {
diagnostic.configure(diagnosticConfiguration.getRight());
try {
diagnostic.configure(diagnosticConfiguration.getRight());
} catch (Exception e) {
var errorMessage = resources.getResourceString(getClass(), "diagnosticConfigurationError",
diagnostic.getInfo().getCode().getStringValue(), e.getMessage());
LOGGER.warn(errorMessage, e);
}
}

return diagnostic;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
diagnosticConfigurationError=Failed to configure diagnostic ''{0}''. Diagnostic will use default configuration: {1}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
diagnosticConfigurationError=Ошибка конфигурирования диагностики ''{0}''. Диагностика будет использовать конфигурацию по умолчанию: {1}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2025
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <nixel2007@gmail.com> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* BSL Language Server is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* BSL Language Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BSL Language Server.
*/
package com.github._1c_syntax.bsl.languageserver.diagnostics.infrastructure;

import com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import com.github._1c_syntax.bsl.languageserver.configuration.diagnostics.DiagnosticsOptions;
import com.github._1c_syntax.bsl.languageserver.diagnostics.BSLDiagnostic;
import com.github._1c_syntax.bsl.languageserver.diagnostics.MagicNumberDiagnostic;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo;
import com.github._1c_syntax.bsl.languageserver.utils.Resources;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

@SpringBootTest
class DiagnosticBeanPostProcessorTest {

@Autowired
private LanguageServerConfiguration configuration;

@Autowired
private Map<Class<? extends BSLDiagnostic>, DiagnosticInfo> diagnosticInfos;

@Autowired
private Resources resources;

@Test
void testPostProcessAfterInitializationWithClassCastExceptionShouldNotCrash() throws Exception {
// given
var diagnosticBeanPostProcessor = new DiagnosticBeanPostProcessor(configuration, diagnosticInfos, resources);
var diagnostic = new MagicNumberDiagnostic();

// Create configuration that will cause ClassCastException
var diagnosticsOptions = new DiagnosticsOptions();
var parameters = new HashMap<String, Either<Boolean, Map<String, Object>>>();

Map<String, Object> configMap = new HashMap<>();
List<String> invalidAuthorizedNumbers = new ArrayList<>();
invalidAuthorizedNumbers.add("-1");
invalidAuthorizedNumbers.add("0");
invalidAuthorizedNumbers.add("1");
configMap.put("authorizedNumbers", invalidAuthorizedNumbers); // This should be a String but is a List

parameters.put("MagicNumber", Either.forRight(configMap));
diagnosticsOptions.setParameters(parameters);

// Use reflection to set the diagnosticsOptions in configuration
Field diagnosticsOptionsField = configuration.getClass().getDeclaredField("diagnosticsOptions");
diagnosticsOptionsField.setAccessible(true);
diagnosticsOptionsField.set(configuration, diagnosticsOptions);

// when/then - should not throw any exception, diagnostic configuration should fail gracefully
assertDoesNotThrow(() -> {
// First set the diagnostic info (postProcessBeforeInitialization)
var result1 = diagnosticBeanPostProcessor.postProcessBeforeInitialization(diagnostic, "testBean");

// Then configure it (postProcessAfterInitialization)
var result2 = diagnosticBeanPostProcessor.postProcessAfterInitialization(result1, "testBean");

// Verify the diagnostic bean is returned (normal behavior even with configuration error)
assertThat(result2).isSameAs(diagnostic);
});

// Verify the diagnostic exists and has info set (basic functionality should work)
assertThat(diagnostic.getInfo()).isNotNull();
assertThat(diagnostic.getInfo().getCode()).isNotNull();
}
}