-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[BUG][JAVA] Since 6.5.0 Generated Lists no longer default to empty List but null #15891
Comments
can you please try the latest master? we recently merged a fix. |
This still fails to generate unfortunately: We still get
We'd want to either initialize to an empty List or when attempting to get or set initialize that list and check for its existence for parity with past behavior |
can you please share a minimal spec to reproduce the issue? |
Spec used openapi: 3.0.3
info:
title: Test API
version: 1.0.0
paths:
/test:
post:
summary: Test
operationId: test
requestBody:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/TestSchema'
responses:
'200':
description: Created
components:
schemas:
TestSchema:
type: object
properties:
options:
description: |
A list of options
type: array
items:
type: string Output model file:
Generated off latest master using command line: Example when generated off of 6.4.0 gradle plugin
Notably I tried this with the 6.4.0 branch checkout using CLI, but it also failed to generate initializing the options to a So to summarize: Gradle 6.4.0 gives the second response (correct response) |
Same issue (Swagger 2.0 specs). |
can you please give it another with 7.0.0-beta released last week? https://github.com/OpenAPITools/openapi-generator/pull/new/7.0.0-beta |
Hi, thanks for following up. Using the Gradle plugin |
FYI just upgraded to I had
now I get
|
Another way is to add The |
Thanks @wing328 that helped a little bit - the model definition now shows
however the associated DTO objects don't seem to be aware of this change - I see the following
Am I missing something? |
Can you please share a spec to reproduce the issue? |
@wing328 Sorry for the delay - will try to code up a spec to help repro today. |
@wing328 For my issue above, all my problems went away when
|
@frankjkelly thanks for confirming the fix. |
I'm seeing this also after moving from 6.3.0 to 7.0.1 but given #14875 this seems like the intended behavior. But the |
We actually get an empty list always and can't get it to null. We have a query parameter defined like - in: query
name: slices
required: false
schema:
type: array
nullable: true
items:
type: string It is set to be a non-required nullable array and config option There's this in ApiClient generated: public List<Pair> parameterToPairs(String collectionFormat, String name, Object value){
List<Pair> params = new ArrayList<Pair>();
// preconditions
if (name == null || name.isEmpty() || value == null) return params; as you can see, when value is null, empty arrayList is returned. This is in 6.6.0 and 7.1.0 |
Hi Any news? Is there a workaround? |
This worked for me #15891 (comment) |
@wing328 is there a reason why optional and required handles differently here: I would expect for both a empty list/set when IMHO we should change this to, because the required field is not important with the
|
can you please submit a PR with the suggested fix? |
Yes I will do |
See here: #18080 |
please give it a try with the latest snapshot version: https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/7.5.0-SNAPSHOT/ you may need a normalizer rule to rollback to previous behavior: #18128 |
Bug Report Checklist
Description
For nullable arrays, our generated POJOs in 6.5.0 (and 6.6.0) no longer generate initialized to an empty ArrayList (e.g
private @Valid List<String> options = new ArrayList<>();
), but instead are generated just as a non-initialized private variableprivate @Valid List<String> options;
Attempts to use
containerDefaultToNull: 'false',
anddefault: []
to hack our way through are not working: The first has no noticeable effect, and the second fails due to the generator failing to import thejava.utils.Arrays
package.openapi-generator version
Using 6.4.0, breaks in 6.5.0 and 6.6.0
OpenAPI declaration file content or url
Generation Details
./gradlew generateApi
task cleanGeneratedApi (type: Delete) {
delete fileTree("${project.buildDir}/generated/api")
}
def openApiSpec = "${project.projectDir}/src/main/resources/api.yml"
// Generate REST API specification
task generateApi( type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask ) {
inputSpec = openApiSpec
outputDir = "${project.buildDir}/generated/api"
templateDir = "${rootDir}/openapi"
invokerPackage = 'com.api'
apiPackage = 'com.api'
modelPackage = 'com.model'
generatorName = 'jaxrs-spec'
configOptions = [
interfaceOnly : 'true',
returnResponse : 'true',
useTags: 'true'
]
// Represent date-time objects as Timestamp rather than Date
typeMappings = [Date : "Timestamp"]
importMappings = ["java.util.Date" : "java.sql.Timestamp"]
}
Steps to reproduce
./gradlew generateApi
Related issues/PRs
Potentially related? #14583
#14959
#14961
Suggest a fix
Check if the property's type is array, initialize it with an empty ArrayList, as before.
The text was updated successfully, but these errors were encountered: