Skip to content

Unsafe add/put calls in model classes #5385

@bbdouglas

Description

@bbdouglas
Description

PR #5363 (fix for issue #5240) introduced unsafe add/put methods on list/map fields. It used to be possible to call add/put on a list/map field without first setting it to an empty list. Now, if you do that, it throws a NullPointerException.

I find it unintuitive that you must call set before you can call add/put. It also breaks backwards compatibility with clients that before were safe just calling add or put without set.

Swagger-codegen version

Current

Swagger declaration file content or url

petstore

Command line used for generation

-l java

Steps to reproduce

If you call, for example, Pet.addTagsItem(item) in the petstore API without first calling Pet.setTagsItem(new ArrayList<Tag>()) then you get a NPE.

Related issues

#5240

Suggest a Fix

I suggest that we add a null pointer check to add/put methods and instantiate the list/map if it is null. This maintains the spirit of issue #5240 and makes what I believe is a more intuitive and safe interface for building up lists/maps.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions