swagger-api / swagger-api/swagger-codegen

[JAVA] Question : How can I generate constructors for model classes?

Open
#8,432 3 comments 0 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

All ,

I am new to swagger and I'd like to add constructors to my generated models. How can I do that?

In the current examples , the generated models provide setters and getters , but my goal is to generate public constructors along with them.

Swagger-codegen version

Version 2.0

Swagger declaration file content or url

I am using Swagger Petstore (Simple)

Command line used for generation

I am using Swagger editor 2 --> Generate Server --> Jaxrs-resteasy

Steps to reproduce
  1. Visit Swagger editor 2
  2. Click in File -> Open Example -> And select petstore_simple.yaml
  3. Click in Generate Server --> Jaxrs-resteasy
  4. From the generated code , open src\gen\java\io\swagger\model\Pet.java

Expected : There is a constructor for the Pet class

Actual : There are no constructors and only setters and getters.

Edit : Reading this I stumbled up the idea that it can generate code for the builder pattern. Could someone please explain how to set this up?

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 reproducing the Swagger Petstore Simple example in Swagger Editor 2 with Generate Server → Jaxrs-resteasy, then inspect src\gen\java\io\swagger\model\Pet.java and the generation templates or configuration used for Java models. Determine how model constructors or builder-style generation is represented, and confirm done when generated models include the requested public constructors without losing the existing getters and setters.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.