Skip to content

fix(#523): handle missing 200 and default response in swagger test generation#1469

Open
phos-web wants to merge 2 commits intocitrusframework:mainfrom
phos-web:issue/523/swagger-generation-response
Open

fix(#523): handle missing 200 and default response in swagger test generation#1469
phos-web wants to merge 2 commits intocitrusframework:mainfrom
phos-web:issue/523/swagger-generation-response

Conversation

@phos-web
Copy link
Contributor

@phos-web phos-web commented Feb 1, 2026

Description
Fixes the Swagger test generator crashing when an API operation defines no 200 or default response. The generator now falls back to the first available response in the spec and resolves its actual HTTP status code, instead of assuming a successful response always exists. If the status code cannot be resolved, the fallback remains 200 OK.

Related issue
Fixes #523

@bbortt bbortt requested review from bbortt and tschlat February 1, 2026 15:38
@bbortt bbortt added Type: Bug Prio: High State: Review If pull-request has been opened an is ready/in review java Pull requests that update java code labels Feb 1, 2026
}

if (response == null) {
Map.Entry<String, Response> firstEntry = operation.getValue().getResponses().entrySet().iterator().next();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't firstEntry null if there are no responses at all? this will throw a NoSuchElementException in that case.

test:

    @Test
    public void shouldCreateClassFromSpecWithNoResponses() throws IOException {
        SwaggerJavaTestGenerator generator = new SwaggerJavaTestGenerator();

        generator.withAuthor("phos-web")
                .withDescription("This is a sample test")
                .usePackage("org.citrusframework")
                .withFramework(UnitFramework.TESTNG);

        generator.withMode(TestGenerator.GeneratorMode.SERVER);
        generator.withSpec("org/citrusframework/swagger/api-with-no-responses.json");

        generator.create();

        verifyTest("PetApiClient_getPetById_IT");
        verifyTestWithStatus("PetApiClient_updatePet_IT", "BAD_REQUEST");
    }

api-with-no-responses.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to

if (response == null && !operation.getValue().getResponses().isEmpty())

try {
httpStatus = HttpStatus.valueOf(Integer.parseInt(firstEntry.getKey()));
} catch (NumberFormatException e) {
httpStatus = HttpStatus.OK;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
httpStatus = HttpStatus.OK;
// Ignore, default value is already HttpStatus.OK;

@phos-web phos-web force-pushed the issue/523/swagger-generation-response branch from 0502b8c to f20c47c Compare February 8, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

java Pull requests that update java code Prio: High State: Review If pull-request has been opened an is ready/in review Type: Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mvn plugin - swagger test generation fails if no "200" or "default" response is given

2 participants