ogen-go / ogen

OpenAPI v3 code generator for go
https://ogen.dev
Apache License 2.0
1.46k stars 87 forks source link

Opt vs OptNil: Generator chooses Opt vs OptNil for nearly equivalent cases, both specifying nullable? #1358

Open fiendish opened 13 hours ago

fiendish commented 13 hours ago

What version of ogen are you using?

$ ogen --version                                                                                                                                                                                                                                            
ogen version v1.8.1 (built with go1.23.2) linux/amd64

Can this issue be reproduced with the latest version?

Yes, 1.8.1

What did you do?

If I do something like this in my openapi spec json file:

"openapi": "3.1.0",
"components": {
  "schemas": {
    "ModelA": {
      "type": "object",
      "required": [],
      "properties": {
        "propA": {
          "type": "object",
          "nullable": true,
          "description": "propA is a property.",
          "$ref": "#/components/schemas/ModelB"
        }
      }
    }
  }
}

The generated output is

type ModelA struct {
    // propA is a property.
    propA OptModelB `json:"propA"`
}

Where the property was nullable, but the output is Opt and not OptNil.

But if I do something like this instead:

"openapi": "3.1.0",
"components": {
  "schemas": {
    "ModelA": {
      "type": "object",
      "required": [],
      "properties": {
        "propA": {
          "type": "object",
          "allOf": [
            {
              "$ref": "#/components/schemas/ModelB"
            }
          ],
          "nullable": true,
          "description": "propA is a property."
        }
      }
    }
  }
}

Then I get the expected OptNil

type ModelA struct {
    // propA is a property.
    propA OptNilModelB `json:"propA"`
}

What did you expect to see?

I expect both cases to be OptNil.

fiendish commented 13 hours ago

I guess it's because $ref siblings are ignored? But I think that should be ok in openapi 3.1? Is 3.1 a no go?

fiendish commented 5 hours ago

I guess it's because $ref siblings are ignored?

Wait, but that doesn't seem right either, because it's picking up the description.