swagger-api / swagger-api/swagger-codegen

Generated compile error: discriminator field is set but not declared

Open
#11,273 1 comment 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

This is the type definition in the json specification file:

{
   "swagger": "2.0",
   ...,
   "definitions":{
      ...,
      "Message":{
         "type":"object",
         "discriminator":"msg_type",
         "title":"Message"
      },

      ...
   }
}

This is the java class that gets generated:

public class Message {
  public Message() {
    this.msgType = this.getClass().getSimpleName();
  }

  ...

The correct generated code would be:

public class Message {
  String msgType;

  public Message() {
    this.msgType = this.getClass().getSimpleName();
  }

  ...

Feels like it would be a simple fix, I'd do it myself but I don't know how. If anyone wants to point me in the right direction I can try and put in a PR.

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

Inspect the Java generator template responsible for discriminator fields and compare it with the generated Message class shown in the issue. Confirm that the discriminator assignment has a corresponding field declaration, then verify that the generated class compiles without the reported error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.