swagger-api / swagger-api/swagger-codegen

[Java] setters for readonly fields are missed

Open
#4,222 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Json parsers like gson need setters for all non transient fields by default. But setters for readonly fields are missed in codegen. So json parser can to use only slow reflection to set value into private read only methods.

Swagger-codegen version

2.2.2-SNAPSHOT

Command line used for generation
java -jar /home/punksta/apps/swagger-codegen-cli.jar generate \
  -i ../api-v1.yml \
  -l java \
Steps to reproduce

read_only for any fields

Related to

https://github.com/swagger-api/swagger-codegen/issues/2189

Suggest a Fix

Easy way: Generate setters, but annotate fields as "read only".
Hard way: Let read_only fields final and generate custom constructors for it. Also need to generate deserialization for every library what invoke custom constructors while json parsing.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Java generator with the provided command and an OpenAPI definition containing a read_only field, then inspect the generated model and its setter behavior. Compare the generated output with the expected Gson use case; done requires a clear, tested approach for handling read-only fields without relying on slow reflection.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.