OpenAPITools / OpenAPITools/openapi-generator

[BUG] Discriminator mapping $refs are not resolved

Open
#9,921 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

From what I understand from the various examples, for polymorphism, we should use oneOf and allOf with the discriminator and mapping props. That works fine when we have only one file. When models are scattered in multiple files, it doesn't work.

After investigating a little bit, I found out that the method DefaultCodeGen.addOneOfInterfaceModel uses ModelUtils.getSimpleRef to get the mappings and that one only handles internal references (ie: "#/components/" and "#/definitions/"), hence, the mappedModels[].modelName in mustache files end up printing ERRORUNKNOWN in the generated code.

Further investigation led me to find that multiple files are merged before the generation begins, so the issue may not be in the addOneOfInterfaceModel method, but the merged file doesn't have the right values in the mapping section. It is something like

  mapping:
    child1: "relative/path/to/child1/yaml/file/from/the/main/schema/file"
    child2: "relative/path/to/child2/yaml/file/from/the/main/schema/file"

instead of

  mapping:
    child1: "#/definitions/path/to/child1/definition"
    # or
    child2: "#/components/path/to/child2/definition"

and there is no reference to child1 and child2 in the merged file

I couldn't find where $refs are resolved to advance further.

openapi-generator version

5.1.1

OpenAPI declaration file content or url
title: parent
type: object
properties:
  objectType:
    type: string
required:
  - objectType
discriminator:
  propertyName: objectType
  mapping:
    child1: "./child1.yaml"
    child2: "./child2.yaml"
title: child1
type: object
allOf:
  - $ref: "./parent.yaml"
properties:
  objectType:
    type: string
    defaultValue: "child1"
  prop1:
    type: string
title: child2
type: object
allOf:
  - $ref: "./parent.yaml"
properties:
  objectType:
    type: string
    defaultValue: "child2"
  prop2:
    type: string
Generation Details

using and generator that supports discriminators would do. I used the aspnetcore one.

Steps to reproduce

running the generator on a file with external $refs in the mappings section

Related issues/PRs

none

Suggest a fix

According to the specs the values in discriminator/mapping props should behave like $ref props (https://swagger.io/specification/#discriminator-object), so a fix would be to properly resolve external paths in discriminator/mapping props.

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 with DefaultCodeGen.addOneOfInterfaceModel and ModelUtils.getSimpleRef, then inspect how external discriminator mapping paths are represented after multiple files are merged. Use the supplied parent, child1, and child2 OpenAPI files with the aspnetcore generator to reproduce ERRORUNKNOWN in generated mustache output. Done means external discriminator mappings resolve to the child model names.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend-api-design, tooling
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.