Skip to content

Support schema references #734

Closed
Closed

Description

@LadnovSasha

Describe the bug
If swagger.json contains a reference like:

"SchemaA": {
    "$ref": "#/components/schemas/SchemaB"
},

python client unable to generate a proper model for this definition

To Reproduce
Steps to reproduce the behavior:

  1. Define a swagger.json that contains a reference to another model
  2. Generate a client openapi-python-client generate --path swagger.json

Expected behavior
Client should contain generated endpoint and contain a model

OpenAPI Spec File

{
	"components": {
		"examples": {},
		"headers": {},
		"parameters": {},
		"requestBodies": {},
		"responses": {},
		"schemas": {
			"SchemaB_string_": {
				"properties": {
					"foo": {
						"type": "string"
					}
				},
				"required": [
					"foo"
				],
				"type": "object",
				"additionalProperties": true
			},
			"SchemaA": {
				"$ref": "#/components/schemas/SchemaB_string_"
			}
		},
		"securitySchemes": {}
	},
	"info": {
		"title": "bar-service",
		"version": "1.0.0",
		"description": "bar service",
		"license": {
			"name": "ISC"
		}
	},
	"openapi": "3.0.0",
	"paths": {
		"/foo": {
			"post": {
				"operationId": "Foo",
				"responses": {
					"200": {
						"description": "Ok",
						"content": {
							"application/json": {
								"schema": {
									"$ref": "#/components/schemas/SchemaA"
								}
							}
						}
					}
				},
				"tags": [
					"foobar"
				],
				"security": [],
				"parameters": [],
				"requestBody": {
					"required": true,
					"content": {
						"application/json": {
							"schema": {
								"$ref": "#/components/schemas/SchemaA"
							}
						}
					}
				}
			}
		}
	},
	"servers": [
		{
			"url": "/"
		}
	]
}

Desktop (please complete the following information):

  • OS: macOS
  • Python Version: 3.9.9
  • openapi-python-client version: 0.13.1

Additional context

Stdout:

Generating bar-service-client
Warning(s) encountered while generating. Client was generated, but some pieces may be missing

WARNING parsing POST /foo within foobar. Endpoint will not be generated.

Could not find reference in parsed models or enums

Reference(ref='#/components/schemas/SchemaA')

Unable to parse this part of your OpenAPI document: 

Reference schemas are not supported.

Reference(ref='#/components/schemas/SchemaB_string_')

Activity

  1. added and removed
    🐞bugSomething isn't working
    on Mar 19, 2023
  2. changed the title [-]Schema $ref is not supported[/-] [+]Support schema references[/+] on Mar 19, 2023
  3. joaonc commented on May 3, 2023

    @joaonc

    Yes please!

    Currently reference schemas are explicitly not supported (here), but it's unclear why as adding this support seems not too difficult and increasingly necessary, especially as more people use TypeScript with models defined in objects (const or type instead of interface). TS linters (Pretty, at least) discourages the use of interface in favor of type, which creates the reference schema.

  4. aletor123 commented on May 22, 2023

    @aletor123

    +1

  5. M1troll commented on May 23, 2023

    @M1troll

    +1

  6. yu-iskw commented on Jun 20, 2023

    @yu-iskw

    +1

  7. authentichamza commented on Jun 21, 2023

    @authentichamza

    +1

  8. paulo-raca commented on Jul 6, 2023

    @paulo-raca
    Contributor

    +1

  9. seanhumbarger commented on Jul 12, 2023

    @seanhumbarger

    +1

  10. Mexflubber commented on Jul 19, 2023

    @Mexflubber

    +1

  11. mikedavidson-evertz commented on Jul 26, 2023

    @mikedavidson-evertz

    +1

  12. rshah-evertz commented on Aug 1, 2023

    @rshah-evertz

    +1

  13. dbanty commented on Aug 13, 2023

    @dbanty
    Collaborator

    Folks seem to really want to vote for features, given all the “+1”, so I’m converting features to discussions so y’all can click the up arrow instead. Then, anyone contributing code can sort by most-voted-for 😁.

  14. added and removed on Aug 13, 2023
  15. locked and limited conversation to collaborators on Aug 13, 2023
  16. converted this issue into a discussion #800 on Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions