Skip to content

Otomatiskan pembuatan OpenAPI (Swagger) spec untuk endpoint publik OpenSID#1680

Open
pandigresik wants to merge 11 commits into
rilis-devfrom
feat/openapi
Open

Otomatiskan pembuatan OpenAPI (Swagger) spec untuk endpoint publik OpenSID#1680
pandigresik wants to merge 11 commits into
rilis-devfrom
feat/openapi

Conversation

@pandigresik

Copy link
Copy Markdown
Contributor

Pull Request: Otomatiskan pembuatan OpenAPI (Swagger) spec untuk endpoint publik OpenSID

Description

Menambahkan konfigurasi Scribe dan CI pipeline untuk menghasilkan OpenAPI 3.0.3 spec secara otomatis dari kode. Spec ini menjadi source of truth untuk contract tests dan onboarding integrasi OpenSID-OpenDK. Menyertakan dokumentasi runbook integrasi testing lengkap.

Changes made:

  1. Konfigurasi Scribe: Setup lengkap config/scribe.php — auth enabled, output OpenAPI 3.0.3, custom logo, extra_info dalam Bahasa Indonesia
  2. Artisan command scribe:copy-openapi: Menyalin spec dari storage/app/scribe/openapi.yaml ke openapi/openapi.yaml (repo root)
  3. Composer scripts: generate-openapi (generate + copy) dan validate-openapi (validasi YAML & struktur)
  4. CI workflow openapi.yml: Job validate (cek keberadaan + validasi spec) dan generate (spin up MySQL, migrate, generate, copy, validate, cek diff, upload artifact)
  5. CI integration di test.yml: Tambahan langkah generate + validate spec setelah test suite berjalan
  6. PHPDoc annotations: Semua controller endpoint publik & OpenSID integration dilengkapi @group, @queryParam, @bodyParam, @response untuk Scribe
  7. Validator script vendor/bin/validate-openapi.php: CLI parser YAML yang memvalidasi struktur spec & menampilkan ringkasan path
  8. OpenAPI spec openapi/openapi.yaml: Spec 3.0.3 mencakup 54 path — autentikasi (JWT), penduduk, pembangunan, APBDes, laporan penduduk, pesan, surat, program bantuan, identitas desa, frontend
  9. Dokumentasi docs/integration-testing.md: Runbook langkah-langkah: build lokal, generate spec, validasi, jalankan test (unit/feature/E2E), docker-compose, contract ZIP validation, API key management, CI pipeline referensi
  10. Scribe view template: Custom Blade template untuk theme dan tryitout JS upgrade (v5.6.0 → v5.11.0)

Reason for change:

  • Poin 1: Belum ada OpenAPI spec yang ter-versioning dan up-to-date untuk endpoint integrasi OpenSID
  • Poin 2: Setiap perubahan endpoint perlu direfleksikan di spec secara otomatis, bukan manual
  • Poin 3: CI harus memvalidasi spec agar tidak ada PR yang merusak kontrak API

Impact of change:

Validasi kontrak API otomatis: Setiap PR memvalidasi bahwa spec konsisten dengan kode
Onboarding lebih cepat: Mitra/developer cukup lihat openapi/openapi.yaml atau /api-docs
Source of truth terpusat: Spec versi 1.0.0 yang di-commit ke repo
Dokumentasi lengkap: Runbook testing mencakup semua skenario (build, test, E2E, rotate key)

Related Issue

#1674

Steps to Reproduce

Before (problem):

  1. Tidak ada spec API yang bisa dijadikan acuan integrasi
  2. Developer harus membaca kode controller untuk memahami kontrak endpoint
  3. Tidak ada validasi otomatis apakah spec sesuai dengan implementasi
  4. ❌ Tidak ada cara standar untuk contract testing

After (solution):

  1. Jalankan composer generate-openapi → menghasilkan openapi/openapi.yaml
  2. Buka /api-docs di browser → dokumentasi interaktif dengan Try It Out
  3. CI memvalidasi spec setiap PR → menjamin spec selalu up-to-date
  4. ✅ Spec versioned di repo sebagai source of truth

Testing on related features:

  • Generate spec ✅ composer generate-openapi menghasilkan YAML valid
  • Validasi spec ✅ composer validate-openapi lulus (54 paths)
  • CI generate ✅ Workflow openapi.yml berhasil generate + validasi
  • Dokumentasi ✅ docs/integration-testing.md lengkap

Checklist

  • I have complied with script writing rules
  • I have followed pull request review process
  • I have created [unit test/integration test] to verify the fix
  • Manual testing has been done in development environment
  • No console errors or warnings
  • Code has been reviewed by [at least 1 person]

Technical Details

Technical Explanation

Scribe Config (config/scribe.php):

  • Auth diaktifkan (Bearer token via API key), default semua endpoint terautentikasi
  • OpenAPI output versi 3.0.3 dengan override info.version = 1.0.0
  • Response calls untuk GET * dengan transaksi database (tidak persist perubahan)
  • Strategies: metadata dari route PHPDoc, URL parameters, request body, response dari response calls/API resources
  • Example languages: bash, javascript, php

Command CopyOpenApiSpec:

  • Membaca dari storage/app/scribe/openapi.yaml (output Scribe untuk tipe laravel)
  • Menyalin ke openapi/openapi.yaml dengan opsi --target kustom
  • Membuat direktori target jika belum ada

CI Workflow openapi.yml:

  • Dua job paralel: validate (ringan, tanpa DB) dan generate (dengan MySQL service)
  • Job generate: import database_test.sql, migrate, scribe:generate, copy, validate, cek git diff
  • Cek diff untuk memberi warning jika spec berubah tanpa di-commit

Controller Annotations:

  • Semua endpoint di app/Http/Controllers/Api/ dan Api/Auth/ dan Api/Frontend/ ditambahi PHPDoc
  • Menggunakan tag @group untuk grouping (Autentikasi, OpenSID Integration)
  • @bodyParam, @queryParam, @response untuk dokumentasi parameter dan response

Configuration changes

  • config/scribe.php — konfigurasi Scribe lengkap
  • .env.example — ditambahkan SCRIBE_AUTH_KEY
  • composer.json — ditambahkan scripts generate-openapi dan validate-openapi

Dependencies added

  • Scribe sudah ada di composer.json sebelumnya (tidak ditambahkan library baru)
  • vendor/bin/validate-openapi.php — script validasi internal (bukan package)

Testing

Manual Testing

  • composer generate-openapi — spec tergenerate tanpa error
  • composer validate-openapi — menampilkan 54 paths, valid YAML
  • Buka /api-docs — halaman dokumentasi interaktif tampil
  • Cek openapi/openapi.yaml — struktur OpenAPI 3.0.3 valid

Automated Testing

  • CI openapi.yml — job validate (tanpa DB) dan generate (dengan DB)
  • CI test.yml — langkah generate + validate setelah test suite

Browser Compatibility (if applicable)

  • N/A — dokumentasi API, bukan UI

Screenshots / Video

image

Before:

Tidak ada OpenAPI spec yang ter-versioning; dokumentasi API hanya dari kode.

After:

openapi/openapi.yaml (945 baris, 54 endpoints) sebagai source of truth.

Breaking Changes

None — ini adalah penambahan fitur dokumentasi, tidak ada perubahan behavior endpoint.

Migration Guide

Not required.

References


Additional notes: PR ini adalah langkah pertama sebelum Issue #1673 (contract tests) dan #1676 (CI pipeline). Spec yang dihasilkan akan menjadi dasar untuk contract testing dengan OpenAPI.

- Update config/scribe.php with proper auth, routes, and OpenAPI config
- Add artisan command scribe:copy-openapi to copy spec to repo root
- Add composer scripts: generate-openapi, validate-openapi
- Add openapi.yaml (initial generated spec with 54 endpoints)
- Add CI workflow: openapi.yml (generate & validate on PR/push)
- Update test.yml to also copy & validate OpenAPI spec
- Add docs/integration-testing.md with local dev instructions
- Add SCRIBE_AUTH_KEY to .env.example
- Fix CekDesa rule: lazy-load DB query in message() instead of constructor
- Add missing test() method to PendudukController
- Regenerate openapi.yaml (now 61 paths, covering all api.php routes)
- Remove outdated note about missing endpoints from docs
- Add @group and @bodyParam/response annotations to all api/v1 controllers
- Add meaningful summaries to frontend API controllers
- Fix AuthController login with proper Scribe annotations
- Regenerate openapi.yaml with all 61 paths properly described
- Each OpenSID submission endpoint now has documented body params & response
- Remove api/frontend/* from scribe route prefixes
- Regenerate spec: 21 paths, focused on OpenSID endpoints only
- Add detailed ZIP column contracts to controller docblocks
- Separate JSON vs ZIP endpoints with proper @bodyParam types
- Add ZIP contract validation table to docs/integration-testing.md
- Add sample ZIP generator script and Pest contract test example
- Each file upload endpoint now documents expected internal file format
@pandigresik
pandigresik requested a review from vickyrolanda July 20, 2026 03:54
@github-actions

Copy link
Copy Markdown
Contributor

🔄 AI PR Review sedang antri di server...

Proses review akan segera dimulai di background — hasil akan muncul sebagai komentar setelah selesai.
Powered by CrewAI · PR #1680

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.

1 participant