swagger-api / swagger-api/swagger-codegen

[nodejs-server] discriminator not taken into account?

Open
#11,111 0 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

When using a discriminator I expect that the generated code will use the discriminator field to determine which class it needs to map to.
It does not seem to do so.

Swagger-codegen version

v3

Swagger declaration file content or url
openapi: 3.0.3
info:
  version: 1.0.0
  title: pet-demo
  description: Pet Demo

paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
              discriminator:
                propertyName: petType
      responses:
        '200':
          description: Updated
components:
  schemas:
    Pet:
      type: object
      properties:
        name:
          type: string
        petType:
          type: string
      required:
      - name
      - petType
    Cat:
      description: A representation of a cat
      allOf:
      - $ref: '#/components/schemas/Pet'
    Dog:
      description: A representation of a dog
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        properties:
          packSize:
            type: integer
            format: int32
            description: the size of the pack the dog is from
            default: 0
            minimum: 0
        required:
        - packSize
Command line used for generation
docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli-v3 generate -i /local/openapi.yaml -l nodejs-server -o /local/out/node
Steps to reproduce

Running the curl command below with a petType Dog should attempt to match the object with the Dog class which should cause a failure since the Dog class requires a packSize property.
However, this returns 200 OK which leads me to believe that it matches the Cat object which does not require any additional properties.
That means that the discriminator field is not being used to map the object to the correct class.

curl --location --request PATCH 'http://127.0.0.1:8080/pets' \
--header 'Content-Type: application/json' \
--data-raw '{
  "name": "charlie",
  "petType": "Dog"
}'

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

Reproduce the issue with openapi.yaml using the nodejs-server Docker generation command and the provided PATCH curl request, then inspect the generated server output for discriminator handling. Done means a Dog payload missing packSize is mapped to Dog and rejected rather than returning 200 OK.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, openapi
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.