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

[dart-dio] Fix compile error in enum properties with "default" values (#20495)

Open chizhanov opened this issue 6 months ago • 2 comments

This PR solves the problem described in #20495.

I made this fix locally and on my openapi the issue was fixed and no new issues were found.

@jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08)

chizhanov avatar May 30 '25 16:05 chizhanov

Do we have this in one of the samples? Probably not...

kuhnroyal avatar Jun 03 '25 16:06 kuhnroyal

Do we have this in one of the samples? Probably not...

I didn't see it, but we can try to add it:

OpenAPI spec:

{
  "openapi": "3.1.0",
  "info": {
    "title": "Simple",
    "version": "0.0.1"
  },
  "paths": {},
  "components": {
    "schemas": {
      "TestEnum": {
        "type": "string",
        "enum": [
          "",
          "1",
          "2"
        ],
        "title": "TestEnum"
      },
      "TestItem": {
        "properties": {
          "test": {
            "type": "integer",
            "title": "test"
          },
          "testEmum": {
            "$ref": "#/components/schemas/TestEnum",
            "default": ""
          }
        },
        "type": "object",
        "required": [
          "test"
        ],
        "title": "TestItem"
      }
    }
  },
  "tags": []
}

And problem will be here:

@BuiltValueHook(initializeBuilder: true)
  static void _defaults(TestItemBuilder b) => b
      ..testEmum = const ._(TestEnum.empty);

chizhanov avatar Jun 03 '25 17:06 chizhanov

@ahmednfwela would be great to get this reviewed ASAP please because it affects us too.

barryguvenkaya avatar Jun 23 '25 04:06 barryguvenkaya

@wing328 can this case be added to any of the samples we use? unfortunately they are all shared with other generators

ahmednfwela avatar Jun 23 '25 10:06 ahmednfwela

@chizhanov did you test it locally to confirm it works for you?

i can add a test or 2 later using a dedicated for dart client tests.

wing328 avatar Jul 03 '25 15:07 wing328

i just merged https://github.com/OpenAPITools/openapi-generator/pull/21516 to the master to have dart dedicated test spec (fake petstore)

can you please merge the latest master into your branch and add a test schema with enum properties (with default values) and regenerate the samples (step 3 in pr checklist) when you've time?

dart test spec: modules/openapi-generator/src/test/resources/3_0/dart/petstore-with-fake-endpoints-models-for-testing.yaml

wing328 avatar Jul 03 '25 16:07 wing328

@wing328 Yes, I tested these changes and in my cases they fixed the problem and did not cause new ones.

I added a test to petstore as you asked.

chizhanov avatar Jul 11 '25 09:07 chizhanov

PR merged

thanks for the contribution

wing328 avatar Jul 17 '25 05:07 wing328

@chizhanov Since this version, code generation broke for me, and I suspect this PR to be the rootcause. I have an enum called ProductTypeSetting in openapi. Before, the default-section was generated like this:

 static void _defaults(SetIsPhysicalDeliveryWriteDtoBuilder b) => b
      ..productTypeSetting = const SetIsPhysicalDeliveryWriteDtoProductTypeSettingEnum._('physical');

After upgrading to 7.15, it is this:

  static void _defaults(SetIsPhysicalDeliveryWriteDtoBuilder b) => b
      ..productTypeSetting = 'physical';

Which does not compile, because it tries to write a string to the enum value.

bw-flagship avatar Oct 10 '25 11:10 bw-flagship

@bw-flagship I think you’re right — it looks like my PR broke this behavior. Could you please share a minimal OpenAPI specification so I can reproduce the issue?

chizhanov avatar Oct 10 '25 18:10 chizhanov

@chizhanov I created a minimal spec to reproduce and put it in an issue: #22120 Let me know if I shall provide further info, thanks for looking into it!

bw-flagship avatar Oct 13 '25 09:10 bw-flagship