OpenAPITools / OpenAPITools/openapi-generator

[BUG] Error using Tests Slice annotation with Spring Boot generated code

Open
#19,360 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Spring boot test slices i.e: @WebMvcTest. @DataJpaTest, etc. don't work properly with the @SpringBootApplication class generated

openapi-generator version

7.7.0

OpenAPI declaration file content or url
Generation Details

The anotation @SpringBootApplication scan by default as

@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

This AutoConfigurationExcludeFilter is used to allow tests that only load an slice of the beans

but in autogenerated class, the ComponentScan is:

@ComponentScan(
    basePackages = {"com.example.demo", "com.example.demo.web" , "com.example.demo"},
    nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class
)

loosing the capability to execute slice tests

Steps to reproduce

You could execute branch master and the tests DemoIssueTests is going to fail

Related issues/PRs
Suggest a fix

You could check fix-issue branch with a template with the fix. I use the field scanBasePackages from SpringBootApplication to define scan packages instead @ComponentScan annotation. You could check that in this branch all test pass properly

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the master branch from the linked reproduction repository and start with the failing DemoIssueTests test. Compare the generated @SpringBootApplication and @ComponentScan setup with the feature/fix-issue branch, focusing on why Spring Boot test slices cannot load correctly. Done means the reproduction tests pass with the generated code.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring-boot
Domain
backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.