Repository navigation
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- 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?
Description
When a query parameter's schema wraps a $ref to a named model in oneOf (or allOf), e.g. oneOf: [{$ref: '#/components/schemas/Price'}], the spring generator leaves out the @RequestParam annotation on the generated method parameter.
Cause:
- After the single-element
oneOf/allOfwrapper is resolved, the parameter is treated as a model, soCodegenParameter.isModel = true. JavaSpring/queryParams.mustacheonly emits@RequestParaminside a{{^isModel}}guard. That guard was added by Link query parameter to model object #2710 to fix [BUG] Query Parameter Serialization (Spring) #2655, so that inline object query params are bound as a model attribute rather than through@RequestParam.- That guard is too broad: it also removes
@RequestParamfrom these wrapped model refs.
Observed CodegenParameter flags for different query param schemas (all isModel=true):
| Query param schema | hasVars |
@RequestParam generated today |
|---|---|---|
oneOf: [$ref: Price] |
false |
no (this bug) |
allOf: [$ref: Price] |
false |
no (this bug) |
direct $ref: Price |
true |
no (unchanged by the proposed fix) |
inline type: object with properties |
true |
no (intended, see #2655) |
openapi-generator version
7.26.0 (also reproduced on current master, 7.27.0-SNAPSHOT)
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: Model Query Param Test
version: 1.0.0
paths:
/items/search:
get:
operationId: itemsSearchGet
tags:
- items
parameters:
- name: SearchTerm
in: query
description: A simple string query param
schema:
type: string
- name: MaxPrice
in: query
description: A model-typed query param via oneOf ref
schema:
nullable: true
oneOf:
- $ref: '#/components/schemas/Price'
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Item'
components:
schemas:
Price:
type: object
description: Represents an amount in a specific currency.
additionalProperties: false
required:
- Currency
- Amount
properties:
Currency:
type: string
description: The currency code.
Amount:
type: number
description: The numeric value.
format: double
default: 0.0
Item:
type: object
properties:
name:
type: string
price:
$ref: '#/components/schemas/Price'Generation Details
openapi-generator generate -g spring -i spec.yaml -o output --additional-properties=interfaceOnly=true
Steps to reproduce
- Save the spec above as
spec.yaml. - Run the generation command above.
- Open
output/src/main/java/org/openapitools/api/ItemsApi.javaand find theitemsSearchGetmethod. - The
maxPriceparameter has no@RequestParamannotation, whilesearchTermdoes.
Actual output
@Parameter(name = "MaxPrice", description = "A model-typed query param via oneOf ref", in = ParameterIn.QUERY) @Valid Price maxPriceExpected output
@Parameter(name = "MaxPrice", description = "A model-typed query param via oneOf ref", in = ParameterIn.QUERY) @Valid @RequestParam(value = "MaxPrice", required = false) Price maxPriceRelated issues/PRs
- [BUG] Query Parameter Serialization (Spring) #2655: original issue, inline object query params incorrectly got
@RequestParam - Link query parameter to model object #2710: added the
{{^isModel}}guard to fix [BUG] Query Parameter Serialization (Spring) #2655, which causes this behaviour - [BUG] [JAVA] Cannot use oneOf as query parameter #18543: related
oneOfquery param issue (client side, different aspect) - [BUG] [Kotlin-Spring]: Free-form parameter should not generate a @RequestParam annotation #14983: opposite problem (
@RequestParamincorrectly present for free-form objects)
Suggest a fix
In JavaSpring/queryParams.mustache, keep the existing {{^isModel}} @RequestParam(...){{/isModel}} branch and add an identical branch for models without vars:
{{#isModel}}{{^hasVars}} @RequestParam(...){{/hasVars}}{{/isModel}}{{^isModel}} @RequestParam(...){{/isModel}}This restores @RequestParam for oneOf/allOf-wrapped model refs (hasVars=false) and leaves inline object params (hasVars=true, #2655) unchanged. Note that a direct $ref to a named model also has hasVars=true, so this fix does not change that case.
Note: kotlin-spring/queryParams.mustache has the same {{^isModel}} guard and shows the same behaviour; it is not covered by this issue.
Activity
[-][BUG][Java][Spring] @RequestParam annotation missing for query parameters using oneOf with $ref to a named model[/-][+][BUG][Java][Kotlin][Spring] @RequestParam annotation missing for query parameters using oneOf with $ref to a named model[/+][-][BUG][Java][Kotlin][Spring] @RequestParam annotation missing for query parameters using oneOf with $ref to a named model[/-][+][BUG][Java][Spring] @RequestParam annotation missing for query parameters using oneOf with $ref to a named model[/+][java][spring] Cover allOf, direct $ref and inline object query param…