Reject an unknown import type when creating an API import

DG
11d ago
+65-14

Stacked on #5697; review after it.

POST /api/v1/imports turned any type outside Import::TYPES into a TransactionImport. A typo like TradesImport answered 201 and the CSV got mapped as transactions. POST /api/v1/imports/preflight already refuses the same value with 422 invalid_import_type, so one request could pass create and fail preflight.

Create now answers 422 invalid_import_type with the same message format as preflight and creates nothing. An omitted or blank type still means TransactionImport, and MonarchImport keeps its own unsupported_type error.

Import.api_creatable_types (every Import::TYPES entry whose class answers api_creatable?, false only for MonarchImport) feeds the error message, the controller and the rswag enum.

The new tests in imports_controller_test.rb and import_test.rb fail without the fix; openapi.yaml regenerated.


Summary by cubic

Rejects unknown import types on POST /api/v1/imports with 422 invalid_import_type instead of silently creating a TransactionImport.

A typo like TradesImport previously answered 201 and mapped the CSV as transactions, while the preflight endpoint already refused the same value. Create now matches preflight and creates nothing. An omitted or blank type still defaults to TransactionImport, and MonarchImport keeps its own unsupported_type error.

  • The error message and the rswag enum read Import.api_creatable_types — the subset of Import::TYPES whose class answers api_creatable? (false only for MonarchImport) — so the error no longer lists MonarchImport as a valid choice. The same api_creatable? check drives the Monarch refusal.
  • Deriving the rswag enum from Import::TYPES also fixes the NameError that broke swaggerize.
  • docs/api/openapi.yaml was regenerated.

Summary by CodeRabbit

  • Bug Fixes

    • Requests with a missing or blank import type now create transaction imports by default.
    • Unsupported types return a 422 error rather than silently creating a transaction import. Monarch imports return an unsupported-type error; other invalid types return an invalid-import-type error.
  • Documentation

    • Updated API documentation to explain the default import type and clarify which types can be created through the API, including the errors returned for unsupported and invalid types.