openapi-python-client icon indicating copy to clipboard operation
openapi-python-client copied to clipboard

Support inline allOf enum

Open skpn opened this issue 4 years ago • 2 comments

Describe the bug In my openapi.json, a parameter for one of my schemas has a string enum as its type, and a member of that enum as its default value. In the generated client it has type Union[Unset, None] and default value UNSET.

To Reproduce Steps to reproduce the behavior:

  1. generate a client based on 'http://sandbox.platform.recital.ai:8000/api/v1/openapi.json'

Expected behavior In the generated client, the schema's parameter should have type Union[Unset, MyEnum] and default value MyEnum.VALUE like in the openapi.json

OpenAPI Spec File http://sandbox.platform.recital.ai:8000/api/v1/openapi.json

Desktop (please complete the following information):

  • OS: Ubuntu 20.04.2 LTS (Focal Fossa)
  • Python Version: 3.8.8
  • openapi-python-client version: 0.8.0

skpn avatar May 10 '21 10:05 skpn

I have the same problem with the latest openapi-python-client==0.10.5. Here is the example of OpenAPI schema:

{
  "openapi": "3.0.2",
  "info": {
    "title": "Example JSON API",
    "version": "0.2.1",
    "description": ""
  },
  "paths": {
    "/api/foo/bar": {
      "post": {
        "operationId": "foo_bar",
        "summary": " ",
        "parameters": [
          {
            "in": "query",
            "name": "item",
            "schema": {
              "title": "Item",
              "type": "string"
            },
            "required": true
          },
          {
            "in": "query",
            "name": "operation_type",
            "schema": {
              "default": "aa",
              "allOf": [
                {
                  "title": "OperationType",
                  "description": "An enumeration.",
                  "enum": [
                    "aa",
                    "bb",
                    "cc"
                  ],
                  "type": "string"
                }
              ]
            },
            "required": false
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "title": "Response",
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {}
  }
}

And here is the generated code:

from typing import Any, Dict, List, Type, TypeVar

import attr

T = TypeVar("T", bound="FooBarOperationType")


@attr.s(auto_attribs=True)
class FooBarOperationType:
    """ """

    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
        foo_bar_operation_type = cls()

        foo_bar_operation_type.additional_properties = d
        return foo_bar_operation_type

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties

Note that the FooBarOperationType class is missing attributes for operation_type.

illagrenan avatar Oct 13 '21 09:10 illagrenan

Not sure if the first example was the same (URL doesn't go anywhere for me) but for the second one, it seems the problem is we don't support inline allOf an enum right now, only references. I'll adjust the issue title accordingly.

dbanty avatar Dec 16 '21 01:12 dbanty