-
Notifications
You must be signed in to change notification settings - Fork 12
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
[PROPOSAL] x-extensible-enum generate real extensible type definition #295
base: master
Are you sure you want to change the base?
Conversation
Example of PR titles that include pivotal stories:
Generated by 🚫 dangerJS |
I do not understand the PRO with this union type: a union between a enum and a string, is a string. When using this type I'll always have to manage both the type, becouse the variable domain is not finite. |
The scope of the type export enum EnumTypeEnum {
"ONE" = "ONE",
"TWO" = "TWO"
}
export type EnumType = t.TypeOf<typeof EnumType>;
export const EnumType = t.union(
[
enumType<EnumTypeEnum>(EnumTypeEnum, "EnumType"),
NonEmptyString
],
"ExtensibleEnumType"
);
const a = EnumTypeEnum.ONE as EnumType;
switch (a) {
case EnumTypeEnum.ONE:
typeof a; // EnumTypeEnum.ONE
break;
default:
typeof a; // EnumTypeEnum.TWO | (string & NonEmptyString)
} Business logic MUST take care of other possible values like Zalando guidelines indicate on point 102 |
Ok, so could be useful to have a sort of "auto-complete" values instead of manage them as string constants. |
Just a general consideration about Zalando guidelines. Zalando sometimes use custom-extensions to manage specific behaviour while dealing with different kinds of edge case in API Design's process. Do we really want to adopt custom-extensions in our specs? I see couple of drawbacks with this choice, last but not least the fact that we are integrating different systems in a complex ecosystem, so we must agree that all systems involved in a particular integration MUST adopt the same guidelines and then provide support with these custom-extensions. It is a strong assumption IMHO |
I will do some consideration. Custom extensions are part of the OAS3 specification (ref) so they don't violate the standard. The introduction of an extension should derive from a specific need (before the adoption of |
: "x-extensible-enum" in looselySource | ||
? looselySource["x-extensible-enum"] | ||
: undefined; | ||
const enumm = looselySource.enum ? looselySource.enum : undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a typo "enumm"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is not a typo, enum
is a reserved word 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! But imho that double "mm" is not very good loking. Anyway, it's just style.
Yep, custom-extensions are allowed by OAS3 specifications :) .I'm bumping discussion about specs because other codegens are more strict on spec parsing so it can be difficult to add support with custom-extensions. BTW I'm wondering about the specs we expose outside our systems. |
I strongly disagree with both the need and the implementation. Implementation In addition, the behavior should be a choice for the client, but it's being declared by the server (which exposes the API spec). Need In my opinion, clients can choose either to perform validation on incoming data or not. Is there a solution in the middle? What would it be? Clients would have to make informed decisions on the kind of misalignment received, to determine whether is acceptable or not. This can only happen with validation rules tailored to every specific case, hence I see no one-size-fits-all behavior. Maybe it would make sense to generate two decoders- one with |
that would be nice, but I won't "let the client choose" instead state (in the docs) that clients MUST use the less strict decoder and the server SHOULD/MUST use the stricter one (I see some parallelism with the |
You are right, the enum is an helper for the client available directly into the generated code. Otherwise the client should manually copy all the possible declared values taken ad example from the description (ref) IMHO an error prone strategy (error into the API definition, error on the client implementation).
The generated code is for the client. The client must handle the "other value" case and the generated type helps the developers to do that. |
don't we use these types on the server part as well? |
Following the whole discussion I think that's right desiring to have an extensible enum that works as extensible, but I agree with others saying that we should not "downgrading" it to a simple NonEmptyString. @balanza had a nice idea about 2 decoders. That could become handy. |
Yes, sorry. Not always the encoder, but mostly the type. |
Following some previous comments, I've changed the generation when the |
Description
Custom extension
x-extensible-enum
generate a type definition asenum
andNonEmptyString
composition.Motivation and Context
When is required extends an API interface adding new value to an enum, the type decoder generated from the previous specification will fail the decode operation. To avoid this behaviour the new decoder will support the enum defined values and any other
NonEmptyString
.How Has This Been Tested?
Unit tests and e2e tests
Screenshots (if appropriate):
Types of changes
Checklist: