Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

feat: generate inline enums #145

Merged
merged 5 commits into from
Sep 15, 2023
Merged

feat: generate inline enums #145

merged 5 commits into from
Sep 15, 2023

Conversation

ctreatma
Copy link
Contributor

@ctreatma ctreatma commented Sep 8, 2023

This enables the RESOLVE_INLINE_ENUMS option for openapi-generator@v7.0.0 to tell the generator to create dedicated models for inline enums instead of treating the inline enums as plain strings. Among other things, generating inline enums helps the SDK properly validate API responses against oneOf schemas in which each component schema has different enum values for the same property name.

In addition to the RESOLVE_INLINE_ENUMS option, this also enables the enumClassPrefix setting. Since the constants used in each enum are defined in the same scope, we have to turn this setting on in order to avoid conflicts between different enums that include identical values.

Fixes #124

Makefile Outdated Show resolved Hide resolved
@ctreatma ctreatma force-pushed the inline-enums branch 2 times, most recently from cfb34c2 to 727a09c Compare September 11, 2023 20:32
@t0mk t0mk self-requested a review September 12, 2023 11:16
@t0mk t0mk marked this pull request as ready for review September 12, 2023 11:16
@t0mk
Copy link
Contributor

t0mk commented Sep 12, 2023

@ctreatma , sorry I clicked on Ready for Review by accident.

@ctreatma ctreatma force-pushed the inline-enums branch 2 times, most recently from 4b62ed2 to b3c7368 Compare September 12, 2023 18:18
@ctreatma
Copy link
Contributor Author

sorry I clicked on Ready for Review by accident.

No worries! I was ready to take it out of draft anyway (or more to the point, I forgot I'd left it as a draft).

Makefile Outdated Show resolved Hide resolved
@ctreatma ctreatma force-pushed the inline-enums branch 2 times, most recently from 3da687d to 4bcd6ec Compare September 14, 2023 16:11
## Enum


* `GLOBAL` (value: `"global"`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the enumClassnamePrefix option isn't used in the docs, so the enum names here are wrong, but IMO that's something we can accept for now and propose a fix upstream. Alternative would be to eject the docs template and customize it but that feels unnecessary here.

Makefile Outdated Show resolved Hide resolved
"inlineSchemaOptions": {
"RESOLVE_INLINE_ENUMS": true
},
"enumClassPrefix": true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add an explicit files key for the model template? I know it works without it, but it might be clearer for people to understand how the model code is generated, if there's a record of what template goes to what files.

I'm actually not sure how it should be specified, maybe sth like

"files:" {
  "model_simple.mustache": {
       "templateType": "model"
       "destinationFilename": "???"
  }
}

.. but I'm now reading through the oag code, and it seems that model_simple is a sub-template of the model template. So it might not even be possible to refer to a custom model_simple expiclitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if that would work, but even if it does I'd be hesitant to make that config change in the scope of this PR, since nothing in this PR is dependent on custom templates.

@t0mk
Copy link
Contributor

t0mk commented Sep 15, 2023

It looks good, and I think it's a useful change. just briefly see my note above #145 (review)

On a bit unrealted note, now that we know how templates work in oag, we could for example do a custom partial_header.mustache, without the #appDescription text, which takes tens of (visual) lines in the beginning of every go source code file.

@ctreatma
Copy link
Contributor Author

ctreatma commented Sep 15, 2023

On a bit unrealted note, now that we know how templates work in oag, we could for example do a custom partial_header.mustache, without the #appDescription text, which takes tens of (visual) lines in the beginning of every go source code file.

Before doing something like this, I think we'd need to improve how our custom templates are managed so that we can easily pull in upstream changes any time we upgrade the generator. I'm not a huge fan of our current patching process (I find it somewhat difficult to reason about and it can be tricky to fix things when patching fails)...but keeping templates up-to-date with the current patching process could look like:

  • instead of custom templates we have patch files
  • we use the openapi-generator author template to eject the templates from the generator
  • apply the patches to the templates
  • then generate the code using the patched templates

Copy link
Member

@cprivitere cprivitere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ctreatma ctreatma merged commit fdd0ef0 into main Sep 15, 2023
5 checks passed
@ctreatma ctreatma deleted the inline-enums branch September 15, 2023 19:09
@github-actions
Copy link
Contributor

This PR is included in version 0.22.0 🎉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate improving generation of enums
4 participants