Skip to content
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

Change api generation #667

Merged
merged 4 commits into from
Aug 25, 2023
Merged

Change api generation #667

merged 4 commits into from
Aug 25, 2023

Conversation

acrantel
Copy link
Member

@acrantel acrantel commented Aug 19, 2023

API generation changes

  • switch from typescript-jquery to typescript-fetch so that our generated enums can be strings.
  • use snake_case in generated types instead of camelCase (to match what backend expects)
  • various changes in backend files to make the generated schema more accurate

Frontend API client changes

  • refactored api.ts and auth.ts into multiple files in utils/api.
  • deleted some legacy functions (especially in compete.ts)
  • made all our api functions use the generated library instead of jquery

Miscellaneous:

  • changed the bat file for generating types: generate-types.bat
  • updated our precommit hooks to exclude the types folder from formatting (see .pre-commit-config.yaml)
  • deleted jquery from package.json

review requests

  • You can ignore the types folder when reviewing these files.
  • please check that the backend schema changes make sense / are accurate

@acrantel acrantel changed the base branch from main to frontend2 August 19, 2023 00:18
@acrantel acrantel force-pushed the api-fetch branch 2 times, most recently from 62c7d07 to 26a8a31 Compare August 19, 2023 00:24
@acrantel acrantel changed the title Api fetch Change api generation Aug 19, 2023
@acrantel acrantel requested a review from n8kim1 August 20, 2023 17:37
n8kim1
n8kim1 previously approved these changes Aug 21, 2023
Copy link
Contributor

@n8kim1 n8kim1 left a comment

Choose a reason for hiding this comment

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

comments are all things to doublecheck, etc -- no feedback that would require a second validation from me
looks good! happy if this just gets revisions and then is merged

(unless we have PRs dismiss on stale)

@@ -8,4 +8,4 @@ python backend/manage.py spectacular --file schema.yml
:: npm install @openapitools/openapi-generator-cli -g
:: if you have a java error, just install java 8 and reload terminal
echo "Generate typescript types from schema"
npx @openapitools/openapi-generator-cli generate -i schema.yml -o frontend2/src/utils/types -g typescript-jquery --additional-properties=modelPropertyNaming=camelCase --additional-properties=disallowAdditionalPropertiesIfNotPresent=false
npx @openapitools/openapi-generator-cli generate -i schema.yml -o frontend2/src/utils/types -g typescript-fetch --additional-properties=modelPropertyNaming=original --additional-properties=disallowAdditionalPropertiesIfNotPresent=false --additional-properties=stringEnums=true
Copy link
Contributor

Choose a reason for hiding this comment

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

what's original vs camelcase? does original just follow what you have
sorry if i missed slack

Copy link
Member Author

Choose a reason for hiding this comment

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

original follows what the backend uses (aka snake_case). if the schema is generated in camelCase it doesn't work b/c the backend expects snake_case

frontend2/src/components/CurrentUserProvider.tsx Outdated Show resolved Hide resolved
frontend2/src/utils/types/.openapi-generator/FILES Outdated Show resolved Hide resolved
frontend2/src/utils/api.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@lowtorola lowtorola left a comment

Choose a reason for hiding this comment

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

Looks really awesome! Left a few comments about stability with future typegen runs but if those are non-issues then ship it!

frontend2/src/utils/api.ts Outdated Show resolved Hide resolved
frontend2/src/utils/api.ts Outdated Show resolved Hide resolved
frontend2/src/utils/api.ts Outdated Show resolved Hide resolved
frontend2/src/utils/api.ts Outdated Show resolved Hide resolved
@acrantel
Copy link
Member Author

this PR is blocked by #671.

@acrantel acrantel force-pushed the api-fetch branch 4 times, most recently from aa49de4 to 986fa3e Compare August 24, 2023 03:20
Copy link
Member Author

@acrantel acrantel left a comment

Choose a reason for hiding this comment

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

still todo: update all references to the API in our frontend components
done!

@@ -149,8 +150,16 @@ def create(self, request, *, episode_id):
headers=headers,
)

@extend_schema(
# https://drf-spectacular.readthedocs.io/en/latest/faq.html#i-m-using-action-detail-false-but-the-response-schema-is-not-a-list
responses=TournamentSubmissionSerializer(many=True)
Copy link
Member Author

Choose a reason for hiding this comment

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

spectacular wasn't recognizing this as a list of tournament submissions, so I added many=True. With many=True, spectacular thought it was a paginated list, so I had to set pagination_class=None in the action decorator below

@@ -409,7 +418,7 @@ def report(self, request, pk=None, *, episode_id):
detail=True,
methods=["post"],
permission_classes=(IsAdminUser,),
serializer_class=None,
serializer_class=EmptySerializer,
Copy link
Member Author

Choose a reason for hiding this comment

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

Spectacular errors if the serializer class is None.

# Split components into request and response parts where appropriate
# This setting is highly recommended to achieve the most accurate API
# description, however it comes at the cost of having more components.
"COMPONENT_SPLIT_REQUEST": True,
Copy link
Member Author

Choose a reason for hiding this comment

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

With this, the requests that we pass into API functiosn do not need to contain readonly fields anymore (e.g. no more passing in id:-1 to createuser)

Copy link
Contributor

Choose a reason for hiding this comment

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

🎉🎉

# ['rest_framework.authentication.TokenAuthentication', ...].
# Empty list ([]) will hide all authentication methods. The default None shows all.
"AUTHENTICATION_WHITELIST": [
"rest_framework_simplejwt.authentication.JWTAuthentication"
Copy link
Member Author

Choose a reason for hiding this comment

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

This gets rid of a lot of the warnings from spectacular that ask us to make an OpenApiAuthenticationExtension for GoogleCloudAuthentication. We only use JWT authentication from the frontend, so we only need that in our schema.
Below is the warning that this line gets rid of:

Warning #0: MatchViewSet: could not resolve authenticator <class 'siarnaq.api.user.authentication.GoogleCloudAuthentication'>. There was no OpenApiAuthenticationExtension registered for that class. Try creating one by subclassing it. Ignoring for now.

@@ -8,4 +8,4 @@ python backend/manage.py spectacular --file schema.yml
:: npm install @openapitools/openapi-generator-cli -g
:: if you have a java error, just install java 8 and reload terminal
echo "Generate typescript types from schema"
npx @openapitools/openapi-generator-cli generate -i schema.yml -o frontend2/src/utils/types -g typescript-jquery --additional-properties=modelPropertyNaming=camelCase --additional-properties=disallowAdditionalPropertiesIfNotPresent=false
npx @openapitools/openapi-generator-cli generate -i schema.yml -o frontend2/src/utils/types -g typescript-fetch --additional-properties=modelPropertyNaming=original --additional-properties=disallowAdditionalPropertiesIfNotPresent=false --additional-properties=stringEnums=true
Copy link
Member Author

Choose a reason for hiding this comment

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

original follows what the backend uses (aka snake_case). if the schema is generated in camelCase it doesn't work b/c the backend expects snake_case

@acrantel acrantel marked this pull request as draft August 24, 2023 16:01
Comment on lines +80 to +83
status.HTTP_200_OK: {
"type": "object",
"additionalProperties": {"$ref": "#/components/schemas/TeamPublic"},
}
Copy link
Member Author

Choose a reason for hiding this comment

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

this schema dict describes the response as an object with arbitrary keys with each value conforming to the schema TeamPublic (which is generated from TeamPublicSerializer)

@acrantel acrantel marked this pull request as ready for review August 24, 2023 19:24
@acrantel acrantel mentioned this pull request Aug 25, 2023
@n8kim1
Copy link
Contributor

n8kim1 commented Aug 25, 2023

seems good, would wait for lowell though

Copy link
Contributor

@lowtorola lowtorola left a comment

Choose a reason for hiding this comment

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

Looks fantastic! Love the split API! Next step, implementing team ranking & ranking history!

@acrantel acrantel merged commit ddfe151 into frontend2 Aug 25, 2023
3 checks passed
@acrantel acrantel deleted the api-fetch branch August 25, 2023 23:48
acrantel added a commit that referenced this pull request Oct 3, 2023
acrantel added a commit that referenced this pull request Feb 8, 2024
acrantel added a commit that referenced this pull request Feb 8, 2024
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.

3 participants