Skip to content

Conversation

fhewitt
Copy link

@fhewitt fhewitt commented Sep 10, 2019

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you written unit tests?
  • Have you written unit tests that cover the negative cases (i.e.: if bad data is submitted, does the library respond properly)?
  • This PR is associated with an existing issue?

Closing issues

This closes #460 by removing the confusion between falsy and incorrect value. On validation we check that the value in enum is found instead of thruty, and we construct the accept list of values correctly.

Allowing empty enum is close to optional enum parameter, but it enforce the user of the API to knowingly pass the empty value instead of simply ignoring it. Moreover, the lack of support is a bug.

Potential Problems With The Approach

Don't see one, but I have very little knowledge of the code base. At least, this didn't break any unit test.

Test plan

None added. Probably should as the code base seems to confuse falsy/invalid, but don't know how.

  1. Adding VALUE_0 = '' as any to testModel.ts > EnumStringValue
  2. Then? I though about checking propertySchema.enum in definition.spec.ts line 263, but the value is undefined.

fhewitt added 2 commits September 10, 2019 10:31
- Do not default to index for enum value if equal to empty string.
- For enum value, reject validation only if value undefined, not all falsy.
Copy link
Collaborator

@dgreene1 dgreene1 left a comment

Choose a reason for hiding this comment

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

I gave instruction on how to write the unit tests.

@@ -298,7 +298,8 @@ export class TypeResolver {

if (extractEnum) {
const enums = enumDeclaration.members.map((member: any, index) => {
return getEnumValue(member) || String(index);
Copy link
Collaborator

Choose a reason for hiding this comment

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

@fhewitt here's a step by step instruction on how to write a unit test for this:

  1. add an enum member to
    VALUE_1 = 'VALUE_1' as any,

So when you're done, it should look like:

export enum EnumStringValue {
  EMPTY = '',
  VALUE_1 = 'VALUE_1' as any,
  VALUE_2 = 'VALUE_2' as any,
}
  1. Do the same for the number enums so we know that this PR doesn't create a regression bug. So add a 0 case for EnumNumberValue here:
    VALUE_1 = 2,
export enum EnumNumberValue {
  VALUE_0 = 0,
  VALUE_1 = 2,
  VALUE_2 = 5,
}
  1. Update the unit tests to check the actual data that is produced from the schema. I would recommend a chai assertion like:
const expectedEnumValues = ['', 'VALUE_1', 'VALUE_2'];
expect(propertySchema.enum).to.eq(true, `for property ${propertyName}[enum]`);

3a) update the string enum tests:

3b) update the nnumber enum tests:

Copy link
Author

@fhewitt fhewitt Sep 10, 2019

Choose a reason for hiding this comment

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

Ok, so I were close, but it fail:

expect(propertySchema.enum).to.eq(expectedEnumValues, `for property ${propertyName}[enum]`);

give

Definition generation for OpenAPI 3.0.0
       for specDefault
         should set additionalProperties to false if noImplicitAdditionalProperties is set to "throw-on-extras" (when there are no dictionary or any types)
           should produce a valid schema for the enumStringArray property on TestModel for the specDefault:
     **AssertionError: for property enumStringArray[enum]: expected undefined to equal [ '', 'VALUE_1', 'VALUE_2' ]**
      at Object.enumStringArray (tests\unit\swagger\schemaDetails3.spec.ts:315:44)
      at Context.<anonymous> (tests\unit\swagger\schemaDetails3.spec.ts:498:49)```

Notice that the added test fail also on the master branch.

The object receive:

{ '$ref': '#/components/schemas/EnumStringValue',
  description: undefined,
  format: undefined,
  nullable: true
}

Template helper use the property.enums value for the function validateEnum. Not sure why this isn't generated for the test...

Copy link
Collaborator

@WoH WoH Sep 10, 2019

Choose a reason for hiding this comment

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

Enums create a ref type.
@fhewitt you can use getValidatedDefinition('EnumStringValue', currentSpec); to get the definition and proceed to do the checks @dgreene1 mentioned on the definition [definitions.spec.ts]

For OAPI3 just check the reference gets created.

Copy link
Author

@fhewitt fhewitt Sep 10, 2019

Choose a reason for hiding this comment

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

The reference check already seems to be there. So, the latest commit should cover the unit test requirement.

Thank you both for your much appreciated explanation and instructions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You're still missing the tests for OpenAPI 3. It's unfortunate that there isn't a getValidatedDefinition in the OpenAPI 3 test file. But you can replicate it by checking the structure of specDefault.spec.components.schemas.EnumStringValue

Thank you for the additional tests and for submitting this PR. I'd really like to see coverage for OpenAPI 3 before we merge this PR since most people will probably be using OpenAPI 3 (since Swagger 2 doesn't support the majority of features).

Copy link
Author

Choose a reason for hiding this comment

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

At the same time, a lot of unit test over the validation are present in definitions.spec.ts but missing in OpenAPI 3. I strongly suspect that it's because the unit tests over validation are good for both cases / all OpenAPI 3 versions, and therefore not required to be duplicated. I can do it (but not today, I fear) if really required.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don’t believe that the missing tests in the OpenAPI test file are intentional. I believe they are missing because OpenAPI 3 was only added to tsoa about 3 months ago. So the test coverage was minimal because it was new. You can even see that in the PR for that file where the original author was like “I hope this works?”

So yes it would be really appreciated if you add the tests. Thank you for your patients. I know that it seems unnecessary but it will really help us maintainers and contributes. :)

Copy link
Author

@fhewitt fhewitt Sep 17, 2019

Choose a reason for hiding this comment

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

Sprint delivered, back here!

You seems right, I've remade an equivalent of getValidatedDefinition and it required enough changes that it's seems to require distinct testing.

Let me know if you see something else.

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.

Empty string in Enum got converted to '0'
3 participants