Skip to content

Add default type for enum #302

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

Merged
merged 1 commit into from
Mar 13, 2023
Merged

Add default type for enum #302

merged 1 commit into from
Mar 13, 2023

Conversation

zorox112-practicum
Copy link
Contributor

@zorox112-practicum zorox112-practicum commented Mar 7, 2023

I made a function for calculating the enum type:

  • if there is a type, we take it
  • if there is no type, check that it is an enum and take its type

@zorox112-practicum zorox112-practicum force-pushed the openapi-add-initial-type-for-enum branch from cda8b00 to e7e0207 Compare March 7, 2023 13:08
@zorox112-practicum zorox112-practicum marked this pull request as ready for review March 7, 2023 13:31
@zorox112-practicum zorox112-practicum mentioned this pull request Mar 7, 2023
if (value.type) {
return value.type;
}
if (value.enum && Array.isArray(value.enum)) {
Copy link
Member

Choose a reason for hiding this comment

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

factor it out into separate function

isEnumOfImplementedType/isEnumOfSupportedType checking if value is one of the supported types

if so return that enum type, otherwise throwing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -234,3 +234,18 @@ function merge(value: OpenJSONSchemaDefinition): OpenJSONSchema {
function isRequired(key: string, value: JSONSchema6): boolean {
return value.required?.includes(key) ?? false;
}

function inferType(value: OpenJSONSchema): JSONSchema6['type'] {
Copy link
Member

Choose a reason for hiding this comment

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

as far as i understand inferType might return undefined yet i can't find you handling that scenario

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR is about adding a type calculation for enum, this function does not handle undefined.

Copy link
Member

Choose a reason for hiding this comment

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

This PR is about adding a type calculation for enum

  1. you are adding function to infer type from value.

  2. this function has undefined as one of its return values

  3. either handle function returning undefined when you call it or throw error instead of returning undefined inside of the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@zorox112-practicum zorox112-practicum force-pushed the openapi-add-initial-type-for-enum branch from e7e0207 to 0a4a64a Compare March 10, 2023 01:15
@@ -20,6 +20,8 @@ const SPEC_RENDER_MODE_HIDDEN = 'hidden';
const SPEC_SECTION_NAME = 'Specification';
const SPEC_SECTION_TYPE = 'Open API';

const SUPPORTED_ENUM_TYPES = ['string', 'number'] as const;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please keep an array here for convenient type generation.

}

function isSupportedEnumType(enumType: JsType): enumType is SupportedEnumType {
return SUPPORTED_ENUM_TYPES.some((type) => enumType === type);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The includes method does not allow using TS

@zorox112-practicum zorox112-practicum force-pushed the openapi-add-initial-type-for-enum branch from 0a4a64a to b989c29 Compare March 10, 2023 12:05
@moki moki merged commit ad4ddae into master Mar 13, 2023
@moki moki deleted the openapi-add-initial-type-for-enum branch October 24, 2023 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants