question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SchemaType is set incorrectly for Simple Enum - SealedChoice vs Choice

See original GitHub issue

Describe the bug Use the following JSON Snippet to generate SDK (I used Typescript SDK. But it does not matter. You can use any language)

{
  "PrintInfo": {
    "title": "PrintInfo",
    "type": "object",
    "properties": {
      "pngData": {
        "type": "string",
        "format": "byte"
      },
      "printMediaType": {
        "type": "string",
        "enum": [
          "ENDLOS",
          "MEHRFAHRTENKARTE"
        ]
      }
    }
  }
}

Expected behavior With the above JSON, I have checked the code model received by the SDK Generator. The SchemaType of ‘printMediaType’ is set to ‘SchemaType.Choice’. It should have been ‘SchemaType.SealedChoice’ (Since this enum is not extensible).

Additional context Basically, a Sealed Choice means (as the name) suggests it is sealed/fixed i.e not extensible. As you mentioned, if you define an enum, then add an x-ms-enum with modelAsString set to false, then it is sealed/fixed (SchemaType.SealedChoice). Autorest Core handles this correctly.

Next scenario is if you define an enum, then add an x-ms-enum with modelAsString set to true, then it is extensible (SchemaType.Choice). Autorest COre handles this scenario also correctly.

The problem is with the last scenario. If you merely define an enum value (with no x-ms-enum property) then it means the enum value is fixed/sealed. The autorest must consider it as SchemaType.SealedChoice. But, it is not doing so. It is considering it as SchemaType.Choice making in extensible. This is the scenario I am trying to get fixed.

@daviwil @timotheeguerin FYI…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JonasMacommented, Aug 5, 2021

Thank you, the $path got it working for us.

For anyone interested in this issue, I got it working using this method for the name generation:

directive:
  - from: openapi-document
    where: $..*
    transform: >-
      if($.enum) {
        $["x-ms-enum"] = {
          modelAsString: false,
          name: (() => {
            const schemeIndex = $path.findIndex(str => str === 'schemas')
            const propertyIndex = $path.findIndex(str => str === 'properties')
            if(propertyIndex >= 0) {
            	const [first, ...rest] = $path[propertyIndex+1]
            	return $path[schemeIndex+1] + first.toUpperCase() + rest.join('');
            } else {
            	return $path[schemeIndex+1];
            }
          })()
        }
      }

The $path is an array which contains the keys of the document down to the enum. To generate unique and descriptive names the name of the schema is combined with the name of the property. If we have an enum outside of an object we only take the schema since the property isn’t defined in this case. In our example this would generate the sealed union type export type PrintInfoPrintMediaType = "ENDLOS" | "MEHRFAHRTENKARTE";

1reaction
timotheeguerincommented, Jul 30, 2021

@JonasMa do you mean the name of the schema? You have access to more context in the transform See here for the available variables.

You could use $path to retrieve the name of the key if that’s what you are looking for.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enums don't match schema: problem with jaxb or xsd?
I'm trying to use JAXB to unmarshal this file into Java objects. I know that there's a problem with SAX in J6 that...
Read more >
Enumeration types - C# reference
You use an enumeration type to represent a choice from a set of mutually exclusive values or a combination of choices.
Read more >
Enums
Reusable enums. In OpenAPI 3.0, both operation parameters and data models use a schema , making it easy to reuse the data types....
Read more >
Using Enums as Django Model Choices | by Ben Cleary
We have run into an issue where engineers are using the wrong values in comparions, we also want to use the values elsewhere...
Read more >
TypeScript: Handbook - Enums
Using enums can make it easier to document intent, or create a set of ... Using an enum is simple: just access any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found