OpenAPITools / OpenAPITools/openapi-generator

Multiple use of allOf not working in python

Open
#6,796 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Python
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

The use of allOf more than once, is not working in python. Specifically, models that use an allOf import on a model that also uses allOf are not correctly constructed.

using

address:      # Can be referenced via '#/components/schemas/address'
  type: object
  properties:
    al:
      type: array
      items:
        $ref: 'https://host014.etsit.upm.es/schemas/test/maitred/msg/net_point.yaml#/network_point'
    did:
      type: string
    sid:
      type: integer
      format: int32

to build

header:
  type: object
  allOf:
    - $ref: 'https://host014.etsit.upm.es/schemas/test/maitred/base/address.yaml#/address'
  properties:
    tag:
      type: string
    f:
      type: integer
      format: int32
      enum:
        - -1
        - 0
        - 1
        - 2
        - 3
        - 4
      default: 0
    ts:
      type: integer
      format: int64

The corresponding generator header class results in

class Header(Model):

    def __init__(self, tag=None, f=0, ts=None, al=None, did=None, sid=None):  # noqa: E501

Everything is fine, including the enumerator.

Now, if it is then used to build

message:
  type: object
  allOf:
    - $ref: 'https://host014.etsit.upm.es/schemas/test/maitred/base/message_header.yaml#/header'
  properties:
    pl:
      type: object

The resulting class results is

class Message(Model):

    def __init__(self, al=None, did=None, sid=None, pl=None):  # noqa: E501

Only the address field are included and components native to header have disappeared.

Its not just a simple template mistake in not including the inheritance of the base class. There is a problem in including the properties in the base model in the derived class. Something is going wrong with the generation of the vars and their variations.

Also, if inheritance was the target design, then there is an ambiguity with the imports. The message model has

from maitred.base.models.header import Header
from maitred.base.models.network_point import NetworkPoint

Where the NetworkPoint should not be necessary at this class level.

A rethink in terms of the constructor arguments will also be needed.

Note, the schema generates a model that, so far works fine in Java.

openapi-generator version

4.3.1-SNAPSHOT and 4.3.0

OpenAPI declaration file content or url

The corresponding full model files are published as:

network_point
address
header
message

Command line used for generation

N/A

Steps to reproduce

N/A

Related issues/PRs

N/A

Suggest a fix

The simplest hack would be to modify the template to inherite the parent class and live with the fact that the base properties on the deepest model will be overloaded, but clarifying the var list would be nice.

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 the published network_point, address, message_header, and gen_message schema files, then compare the generated Header and Message classes. Trace how nested allOf properties, constructor variables, and imports are represented, and verify that the generated Message includes the expected header fields without unnecessary imports.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.