Skip to content

buildResponse throws NoSuchElementException on unresolved (external/multi-file) response $ref #379

Description

@joseegman-idoneea

Summary

MapperPathUtil.buildResponse throws java.util.NoSuchElementException: No value present when an operation response is declared via a $ref that is not present in the pre-built response map — e.g. a response referenced from an external/multi-file location. The generator aborts instead of resolving the reference (or degrading gracefully).

Environment

  • Plugin: com.sngular:scs-multiapi-maven-plugin (reproduced on the 6.3.x/6.4.0 engine code)
  • OpenAPI: 3.0/3.1, multi-file layout

Reproduction

An operation whose response is a $ref to a response defined outside the main file's components/responses, e.g.:

responses:
  '200':
    $ref: './responses/ServiceTypes.yml'

Generation fails with:

java.util.NoSuchElementException: No value present
    at java.util.Optional.orElseThrow (Optional.java:377)
    at com.sngular.api.generator.plugin.openapi.utils.MapperPathUtil.buildResponse (MapperPathUtil.java:347)
    at com.sngular.api.generator.plugin.openapi.utils.MapperPathUtil.mapResponseObject (MapperPathUtil.java:327)
    at com.sngular.api.generator.plugin.openapi.utils.MapperPathUtil.createOperation (MapperPathUtil.java:145)
    ...

Root cause

In buildResponse:

if (ApiTool.hasRef(response)) {
  realResponse = globalObject.getResponseNode(MapperUtil.getRefSchemaKey(response)).orElseThrow();
}

globalObject.getResponseNode(...) only contains entries built from the main openapi.yml's components/responses (see ApiTool.getResponseSchemas / getComponentSchemasByType, keyed RESPONSES/<snake_name>). A response $ref that points to an external file (or whose key does not match) yields an empty Optional, and orElseThrow() aborts the whole generation.

Suggested fix

When the response ref is not found in the response map, fall back to resolving it from the filesystem (as getRefSchema already does for schemas via SchemaUtil.solveRef), and if it still cannot be resolved, degrade gracefully (treat as a no-content response) rather than throwing. Content-less responses (204/401/403/500, description only) must remain valid — mapContentObject already tolerates a null content.

Notes

A reproduction fixture (operation with an external response $ref, plus no-body responses) will be added with the fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions