OpenAPITools / OpenAPITools/openapi-generator

[BUG][codegen] Do not use the "title" attribute to control code generation

Open
#5,248 7 comments 4 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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

IMO, the "title" attribute in a OAS document should not have any impact whatsover on the code generation (codegen currently uses title to flatten inline schemas). Using 'title' can lead to completely unexpected outcomes for the generated code.

The OAS spec clearly states the 'title' attribute is for human consumption, so codegen should not use it.

I was working with a draft OAS document that has a composed allOf schema. The "title" of the inner schema happens to be the name of the outer (composed) object, but it could also have been any value, including names of totally unrelated schema.

That had the weird side effect to nullify the inheritance hierarchy. Then you have to debug the code to finally understand why the "parent" attribute ends up being nullified as a side effect of the "title" attribute

Currently, codegen uses the "title" attribute in the logic to flatten composed schemas with inline models. When a OAS model is defined inline, the generator flattens the model and tries to use the inlined "title" attribute in the OAS document to create a model name. If "title" is not present in the spec, it uses the hard-coded 'InlineObject'' model name.

The logic is at
https://github.com/OpenAPITools/openapi-generator/blob/4208f3da828fc34d1a2a1628443a7ca06920e2c6/modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java#L160

openapi-generator version

master February 8th 2020

OpenAPI declaration file content or url

Consider the following OAS document. It is meant to model an inheritance hierarchy, where some properties are inherited from "os.BaseConfig", plus the XYZ property which is defined as an inlined object.

In Python, the generated name of the outer class is "OsInstall", because the code generator applies a sanitization and camel casing transformations. Furthermore, the inline object does not have "title", in which case the code generator artificially creates a schema with a name hard-coded to "InlineObject". A log message warns to set the "title" attribute.

    os.Install:
        allOf:
            - $ref: '#/components/schemas/os.BaseConfig'
            - type: object
              properties:
                  XYZ:
                          type: string

If, on the other hand, the OAS spec includes the "title" attribute in the inline object, the code generator still creates a schema for the inline object, and the name of that schema is now based on the "title" attribute. This is a really surprising feature. IMO, at a minimum we should log a warning to indicate the user that the code generator uses the title attribute.

    os.Install:
        allOf:
            - $ref: '#/components/schemas/os.Abstract'
            - type: object
              title: OS Install
              properties:
                  Name:
                          type: string

In the case when the title attribute is used, the name of the generated schema does not use the same transformation function. The generated name ends up being "OSInstall", unlike the outer class name which is "OsInstall". Of course, the author is supposed to write whatever title they want, so it could be "os installation", "the OS installation configuration file", or any other string that makes sense for humans. The author does not know this will be used for code generation.

In the case of Python, the two strings "OSInstall" versus "OsInstall" cause a import failure when the SDK is executed.

An even more unexpected result is with the following OAS schema. The inner schema within 'allOf' has a "title attribute, and its value happens to match the name of the outer schema.

    os.Install:
        allOf:
            - $ref: '#/components/schemas/os.Abstract'
            - type: object
              title: os.Install
              properties:
                  Name:
                          type: string

Without the "title" attribute, the generated Java class is:

class OsInstall extends OsAbstract

With the title attribute set to "os.Install", the inheritance is no longer generated, which is really weird.

class OsInstall

Potentially, the value of the "title" attribute could be the name of a completely unrelated schema.

Command line used for generation
Steps to reproduce
Related issues/PRs

#843

Suggest a fix

Don't use the "title" attribute to generate a unique model name.

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 modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java around line 160 and review how inline schemas use the OAS title. Reproduce the examples for composed allOf schemas in Java and Python, then verify that title values no longer alter generated model names or inheritance relationships.

Written by the indexing model from the issue text.

Assessment

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