OpenAPITools / OpenAPITools/openapi-generator

[BUG] Unable to generate examples, either inline or from $ref

Open
#20,009 0 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? -- running the latest code in a corporate environment is challenging due to proxy and external dependency restrictions.
  • 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

examples using $ref is not supported. e.g. from https://swagger.io/docs/specification/v3_0/adding-examples/:

    content:
      application/json:
        schema:
          $ref: '#/components/schemas/MyObject'
        examples:
          objectExample:
            $ref: '#/components/examples/objectExample'
    ...
    components:
      examples:
        objectExample:
          value:
            id: 1
            name: new object
          summary: A sample object

There are 2 problems with this specification:

  1. The $ref is not expanded to provide the value from the indicated location and instead contains the literal path string
  2. There is no way to reference the value in the templates, due to a similar bug as #18293 (multiple values are represented as a map, and the key is not exposed in the individual items, requiring knowledge of the specific key values to access them).

Note: using example (note singular) does not support $ref at all, according to the OpenAPI 3 spec. This issue is specific to the collection style of examples.

openapi-generator version

7.9.0

OpenAPI declaration file content or url

See above YAML.

Generation Details

Applies to all generators. The values are not accessible through the mustache templates at all, without prior knowledge of the key names, limiting the utility of this feature.

Steps to reproduce

See above YAML.

Related issues/PRs

#18293 has the same cause, for a different part of the API model.

Suggest a fix
  1. Expose the map key as a property on the collection items
  2. expand the $ref to provide the dereferenced value.

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 the YAML example and trace how the collection-style examples are exposed to the mustache templates. Read related issue #18293 for the similar map-key problem. Done means $ref values are dereferenced and collection item keys are available to templates, with behavior working across generators.

Written by the indexing model from the issue text.

Assessment

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