openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Using `oneOf` and `properties` generates incorrect code

Open MattGson opened this issue 1 year ago • 2 comments

Describe the bug Using oneOf and properties treats properties as another oneOf case, instead of merging these fields in.

  "ComposedSchema": {
        "type": "object",
        "oneOf": [
          {
            "type": "object",
            "required": [
              "data1"
            ],
            "properties": {
              "data1": {
                "$ref": "#/components/schemas/MySchema1"
              }
            },
            "additionalProperties": false
          },
          {
            "type": "object",
            "required": [
              "data2"
            ],
            "properties": {
              "data2": {
                "$ref": "#/components/schemas/MySchema2"
              }
            },
            "additionalProperties": false
          }
        ],
        "required": [
          "field1",
          "field2"
        ],
        "properties": {
          "field1": {
            "type": "integer",
            "format": "uint16",
            "minimum": 0.0
          },
          "field2": {
            "type": "integer",
            "format": "uint8",
            "minimum": 0.0,
            "nullable": true
          }
        }
      },

Generates:

export type ComposedSchema =
	| {
			data1: MySchema1;
	  }
	| {
			data2: MySchema2;
	  }
	| {
			field1: number;
			field2?: number | null;
	  };

But should generate:

export type ComposedSchema =
	| {
			data1: MySchema1;
                       	field1: number;
			field2?: number | null;
	  }
	| {
			data2: MySchema2;
			field1: number;
			field2?: number | null;
	  };

MattGson avatar Dec 18 '23 00:12 MattGson

The generator still works using the nullable: true key. This is a workaround, especially if you want to use OpenAPI 3.1

MichaelCereda avatar Feb 20 '24 10:02 MichaelCereda

@MattGson I got this use case fixed in our fork which aims to support OpenAPI 3.1. Would you mind giving it a try and see if it works for you?

mrlubos avatar Feb 20 '24 12:02 mrlubos