Skip to content

nonDbFakerStub on items with ref#112

Merged
cebe merged 2 commits into
masterfrom
nonDbFakerStub
May 28, 2026
Merged

nonDbFakerStub on items with ref#112
cebe merged 2 commits into
masterfrom
nonDbFakerStub

Conversation

@siggi-k
Copy link
Copy Markdown

@siggi-k siggi-k commented May 27, 2026

Fix: FK faker detection, JSONB sub-property key derivation, and allOf handling

1. FK detection via attribute->reference instead of _id suffix (FakerStubResolver.php)

The _id-suffix heuristic generated invalid PHP for JSONB sub-properties with a $ref
(e.g. payment_methodpayment_method_id, but attribute->reference was empty → \common\models\::find()).
Now uses !empty($this->attribute->reference) — set only when an actual $ref/allOf[$ref] resolves to a model.

Before (workaround — plain integer, no $ref, FK relation not expressed in spec):

payment_method_fields:
  type: object
  x-db-type: jsonb
  properties:
    payment_method_id:
      type: integer

After (correct — $ref used directly, key payment_method_id derived automatically):

payment_method_fields:
  type: object
  x-db-type: jsonb
  properties:
    payment_method:
      $ref: '../openapi.yaml#/components/schemas/CompanyPaymentMethod'

Also works with allOf (e.g. to add a description):

payment_method_fields:
  type: object
  x-db-type: jsonb
  properties:
    payment_method:
      allOf:
        - $ref: '../openapi.yaml#/components/schemas/CompanyPaymentMethod'
        - description: "Payment method used for this invoice"

2. New private resolveElement() — correct key derivation for JSONB sub-properties (FakerStubResolver.php)

fakeForObject() used the raw spec property name as key. FK sub-properties now go through
resolveElement(), which runs AttributeResolverAttribute::asReference() — the same
_id suffix logic as for top-level FK columns. Returns both columnName and fakerStub.
aElementFaker() delegates to it.

Using the YAML from point 1, the generated faker code is:

$model->payment_method_fields = [
    'payment_method_id' => $faker->randomElement(\common\models\CompanyPaymentMethod::find()->select("id")->column()),
    // ...
];

3. fakeForObject depth default: 1 → 0; reindentArrayMapForObject line shift fixed (FakerStubResolver.php)

Default 1 caused wrong indentation for top-level object attributes. Now 0; fakeForArray
explicitly passes 1. reindentArrayMapForObject now shifts all continuation lines (not just
the first), and }, range(1, n) is written on one line.

4. allOf handling: elseif chain → independent if blocks (PropertySchema.php)

The elseif chain applied only the first matching optional attribute from an allOf entry.
Four independent if blocks now apply all present attributes in one pass.

@cebe cebe merged commit 47ab080 into master May 28, 2026
5 checks passed
@cebe cebe deleted the nonDbFakerStub branch May 28, 2026 09:19
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