Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct way of using isinstance with this library? #53

Closed
jtreminio opened this issue Jan 11, 2025 · 2 comments
Closed

Correct way of using isinstance with this library? #53

jtreminio opened this issue Jan 11, 2025 · 2 comments

Comments

@jtreminio
Copy link

Seems depending on the OAS version used, a different set of libraries will be loaded v3_0 vs v3_1.

This is fine, and a great help, but I seem to be running into issues when testing an object with isinstance.

With the following simple OAS

openapi: 3.1.0
info:
  version: 1.0.0
  title: Fixture - Single requestBody
tags:
  - name: pet
paths:
  /single_request_body:
    post:
      tags:
        - pet
      operationId: single_request_body
      responses:
        '200':
          description: Successful operation
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
  /multiple_request_body:
    post:
      tags:
        - pet
      operationId: multiple_request_body
      responses:
        '200':
          description: Successful operation
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Customer'
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
components:
  schemas:
    Customer:
      properties:
        id:
          type: integer
          example: 100000
      type: object
    Pet:
      properties:
        id:
          type: integer
          example: 10
      type: object

and the following code:

import openapi_pydantic as oa

# define path to oas_file 
oas_file = ""

oa_data = oa.parse_obj(oas_file)
customer = oa_data.components.schemas.get("Customer")
pet = customer.properties.get("pet")

if isinstance(pet, oa.Reference):
    print(1)
else:
    print(0)

When openapi is 3.1.0 the isinstance check prints 1, but when it is 3.0.2 then we get 0.

Is there a better way to handle this, without knowing ahead of time the version of the OAS to be loaded?

Right now I've fallen back to using if hasattr(pet, "ref") when I want to check if the current object is a Reference, but this doesn't scale very well.

Suggestions?

@mike-oakley
Copy link
Owner

mike-oakley commented Jan 12, 2025

Hi @jtreminio - you're right that the top-level imports default to the latest version (3.1.x) objects for convenience. However both the 3.0 and 3.1 objects can also be imported explicitly from openapi_pydantic.v3.v3_0 and openapi_pydantic.v3.v3_1 too.

You should therefore either be able to use the isinstance check against the explicit version of Reference that you are expecting (in this case from openapi_pydantic.v3.v3_0 import Reference), or you can import both the v3_0 and v3_1 models and use the tuple form to check both models (ie isinstance(obj, (v3_0.Reference, v3_1.Reference))).

Hope that helps!

@jtreminio
Copy link
Author

Thank you, that's what I figured would be the case.

Love this project, thank you for the amazing work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants