openapi-generator: [BUG][Python] Attribute-style access fails for model properties
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
See the spec below for the model definitions. I was under the impression the following code is legal, but it raises an error:
from openapi_client.models import Foo
f = Foo(bar='bar')
f.bar # this raises an AttributeError
f['bar'] # this works fine
openapi-generator version
v6.2.1, also tested on master
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: APMS
version: '0.1'
paths:
/foo:
post:
tags:
- test
summary: Test
operationId: create_foo
responses:
'200':
description: ok
components:
schemas:
Foo:
type: object
properties:
bar:
type: string
example: foobar
Generation Details
docker run -v $PWD/spec.yaml:/spec.yaml -v $PWD/client:/client -it openapitools/openapi-generator-cli:latest generate -i /spec.yaml -g python -o /client
Steps to reproduce
Generate the spec above
Related issues/PRs
Tried, but couldn’t find anything.
Suggest a fix
After looking at the generated code for a while, I found a solution. In https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/schemas.handlebars#L1721 , changing
if name not in self.__class__.__annotations__:
to
if name not in self.MetaOapg.properties.__annotations__:
seems to resolve the problem. However, since there’s a lot of __new__ magic in the generated code, I’m not sure if this is a real fix.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (7 by maintainers)
@ashuk203 this is by design. It already does what you are asking for. Your instance is an instance of type DynamicSchema which is a class that is built on the fly of all validated schemas including the response body schema. Is it is an instance of that spec defined response body class. You can see examples of this here: https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/petstore/python/tests_manual/test_fake_api.py#L59
This is described in the migration guide
All validated data is instantiated in an instance that subclasses all validated Schema classes and Decimal/str/tuple/frozendict/NoneClass/BoolClass/bytes/io.FileIO2.0.2 was released with the fix
Dot referenced properties only exist when:
When those two conditions are met, a dot referenced property is defined. Otherwise one must access the value by the key name. Per item 7 in the readme: https://github.com/OpenAPITools/openapi-generator/tree/v6.6.0/samples/openapi3/client/petstore/python#changes
Note: All optional keys may not exist, so properties are not defined for them
Oh I see, I didn’t look at that part of the docs, so thanks for linking it. I think you can consider this issue resolved now.