openapi-to-graphql icon indicating copy to clipboard operation
openapi-to-graphql copied to clipboard

Fields takes precedence over types

Open Zn4rK opened this issue 2 years ago • 0 comments

Describe the bug

It seems to me like the fields in a schema takes precedence whenever a field references a type that already exists. For inline objects, it seems reasonable to generate the type name, but pure references should take priority - no?

To Reproduce Use this schema:

{
  "info": {
    "title": "Test",
    "version": "0.0.0"
  },
  "openapi": "3.0.2",
  "paths": {
    "/user": {
      "get": {
        "description": "Doesn't matter",
        "tags": [
          "user"
        ],
        "responses": {
          "200": {
            "description": "A user",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/User"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "User": {
        "type": "object",
        "properties": {
          "something": {
            "type": "string"
          },
          "thing": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Thing"
            }
          }
        }
      },
      "Thing": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

And generate the schema. The end result will look like this:

type Query {
  user: User
}

type Thing2 {
  name: String
}

type User {
  something: String
  thing: [Thing2]
}

Expected behavior I would expect the schema to look like this:

type Query {
  user: User
}

type Thing {
  name: String
}

type User {
  something: String
  thing: [Thing]
}

I.e it shouldn't create a new type since it has one specified in the component/schema section.

Additional context I've tried to workaround this with various options, but to no avail. My solution as of right now is to do some pre-processing on the schema, and then fix it with schema wrapping.

I can probably identify and fix this bug if it's declared a bug.

Zn4rK avatar Jul 04 '22 18:07 Zn4rK