question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][python] Cannot instantiate class without required read-only parameters for POST endpoint

See original GitHub issue

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)

@spacether

Python generator

Description

I have a POST endpoint and the request body and response body use the same schema definition as below. Dataset has multiple readOnly parameters that must be present in the response, but should not be provided in the request.

I can instantiate the Dataset model providing None to the parameters that are readOnly and without verifying the model’s input (_check_type=False). However, post request still fails (404 bad request) because params are still present with None values.

openapi-generator version

5.1.1

OpenAPI declaration file content or url
paths:
  /datasets:
    post:
      operationId: v1_inference_datasets_post
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Dataset'
        description: The dataset to be registered
        required: true
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Dataset'
          description: Confirmation of dataset registration
      summary: Register a dataset
      tags:
        - datasets
 
components:
  schemas:
    Dataset:
      additionalProperties: false
      example:
        statusLastChange: 2000-01-23T04:56:07+00:00
        modifiedOn: 2000-01-23T04:56:07+00:00
        name: name
        inactiveOn: 2000-01-23T04:56:07+00:00
        description: description
        mediaType: text/plain
        id: 0
        createdOn: 2000-01-23T04:56:07+00:00
        status: active
      properties:
        id:
          description: ID of the dataset
          readOnly: true
          type: integer
        name:
          description: User-defined name of the dataset
          maxLength: 128
          type: string
        description:
          description: User-defined description of the dataset
          maxLength: 256
          type: string
        mediaType:
          description: Data type of the dataset. All data must be of the same type.
          enum:
            - text/plain
          type: string
        createdOn:
          description: Creation time of the dataset
          format: date-time
          readOnly: true
          type: string
        modifiedOn:
          description: Modification time of the dataset
          format: date-time
          readOnly: true
          type: string
        inactiveOn:
          description: |
            Time when the dataset will become inactive. Make sure to retrieve all data and job results
            before this time.
          format: date-time
          readOnly: true
          type: string
        status:
          description: |
            Status of the dataset:
              * `active`: The dataset can be modified (including the data it contains).
              * `committed`: The dataset can no longer be
                 modified (including the data it contains) but jobs can be submitted.
              * `inactive`: The dataset can no longer be modified (including the
                data it contains) and jobs cannot be submitted. Once a dataset is inactive,
                it becomes a candidate for deletion meaning it will soon be deleted from the system.
          enum:
            - active
            - committed
            - inactive
          readOnly: true
          type: string
        statusLastChange:
          description: Time when the status last changed
          format: date-time
          readOnly: true
          type: string
      required:
        - createdOn
        - description
        - id
        - inactiveOn
        - mediaType
        - modifiedOn
        - name
        - status
        - statusLastChange
      type: object
Generation Details

generate -i openapi/openapi.yaml -g "python" --package-name ex_ai_hub_api_client2 -p packageVersion=0.2.0

Steps to reproduce
Related issues/PRs
Suggest a fix

I suggest the following 3 modifications:

  1. Modify the signature of __init__ for readOnly=True parameters from
Dataset:
    @convert_js_args_to_python_args
    def __init__(self, id, name, description, created_on, modified_on, inactive_on, status, status_last_change, *args, **kwargs):

to

Dataset:
    @convert_js_args_to_python_args
    def __init__(self, id, name, description, created_on=None, modified_on=None, inactive_on=None, status=None, status_last_change=None, *args, **kwargs):
  1. Modify the checks accordingly to support null values when instantiating a class
  2. Do not add parameter to body payload for readOnly=True and nullable=False parameters when value is None .

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
gbmarc1commented, May 13, 2021

Solved in #9409

1reaction
gbmarc1commented, Apr 23, 2021

ok sounds a good plan. I can try to implement that next week

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python cannot instantiate an imported class - Stack Overflow
This class is to handle some-backend related processes, and arguments only contain back related variables. Also, this class is on the different ...
Read more >
Cannot instantiate a class with parameterized generic types
The issue is that I need a generic class with some Type T that internally instantiates another class with the same generic type....
Read more >
How to Resolve the Instantiation Exception in Java - Rollbar
The InstantiationException in Java is thrown when the JVM cannot instantiate a Java type at runtime using the Class.newInstance() method.
Read more >
API Reference Manual — param v1.12.3
Setting a Parameterized class attribute to be a Parameter instance causes that attribute of the class (and the class's instances) to be treated...
Read more >
Accessing the Incident Rules Page Fails with ADFC-10001 ...
ErrorPopupUtil logp.251 - ADFC-10001: cannot instantiate class 'oracle.sysman.core.event.rules.ui.view.IncidentRulesUIBean' javax.faces.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found