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

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

Open
fiendish opened this issue Dec 3, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@fiendish
Copy link

fiendish commented Dec 3, 2024

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:

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

or

"components": {
  "schemas": {
    "ModelA": {
      "type": "object",
      "required": [],
      "properties": {
        "propA": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "type": "object",
                "allOf": [
                  {
                    "$ref": "#/components/schemas/Hub.CarrierIntegration.Addresses.Address"
                  }
                ]
              }
           ],
          "description": "propA is a property.",
          "$ref": "#/components/schemas/ModelB"
        }
      }
    }
  }
}

or

(I know this isn't true 3.0 or 3.1 but ogen doesn't complain about it? 😄)

"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:

"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"`
}

This is weird because ogen does apply other siblings to $ref, which I believe is an oapi 3.1 behavior.

What did you expect to see?

I expect both cases to be OptNil.

@fiendish fiendish added the bug Something isn't working label Dec 3, 2024
@fiendish fiendish closed this as completed Dec 3, 2024
@fiendish fiendish reopened this Dec 3, 2024
@fiendish
Copy link
Author

fiendish commented Dec 4, 2024

N.b. I ran into this by using the discovered workaround mentioned in #1338 which I guess is no longer a good workaround.

@fiendish
Copy link
Author

fiendish commented Dec 4, 2024

Also, I'm definitely not an expert in how OpenAPI needs to be structured to be semantically correct at every version, especially around $refs and allOf/oneOf, which seem somewhat cumbersome. So if I'm just doing it wrong that will be good to know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant