Skip to content

feat: adjust reflection#62

Merged
akfaiz merged 6 commits into
mainfrom
feat/adjust-reflection
May 11, 2026
Merged

feat: adjust reflection#62
akfaiz merged 6 commits into
mainfrom
feat/adjust-reflection

Conversation

@akfaiz

@akfaiz akfaiz commented May 11, 2026

Copy link
Copy Markdown
Member

Description

  • Add InterceptPropFunc to ReflectorConfig
  • Add InterceptSchemaFunc to ReflectorConfig
  • Add RequiredPropByValidateTag option
  • Fix sanitizeDefName: skip package prefix for types from "main" package
  • Fix uint32 and uint format from int32 to int64 (uint32 max 4.3B exceeds int32 max 2.1B)

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactor / code quality
  • Other (describe):

Checklist

  • make check passes locally (sync + tidy + lint + test)
  • Tests added or updated to cover the change
  • Golden files updated if spec output changed (make test-update)
  • Relevant documentation updated (README, adapter README, etc.)
  • Commit messages follow Conventional Commits

akfaiz added 3 commits May 11, 2026 19:13
…h reflection edge cases

- Add InterceptPropFunc and InterceptPropParams to ReflectorConfig; hook called
  pre/post per struct field in StructSchema (return ErrSkipProperty to skip)
- Add InterceptSchemaFunc and InterceptSchemaParams to ReflectorConfig; hook called
  pre/post per type in SchemaForType (inline/primitive) and RefSchema (components);
  pre-call stop=true overrides default schema generation
- Add RequiredPropByValidateTag option: marks fields required when validate tag
  contains "required"; configurable tag name and separator
- Fix sanitizeDefName: skip package prefix for types from "main" package
- Fix uint32 and uint format from int32 to int64 (uint32 max 4.3B exceeds int32 max 2.1B)
- Fix RefSchema pre-hook: assign StructSchema fields onto existing pointer so
  pre-hook customizations (e.g. Extensions, Description) survive to post-hook
- Fix ApplyNullable (3.1+): merge "null" into existing []string type instead of
  silently skipping when schema.Type is already a multi-type slice
- Add tests for all new hooks, RequiredPropByValidateTag, and patched edge cases
- Fix Reflected Go Types table: uint8/uint16 use int32, uint/uint32/uint64/uintptr use int64
- Add InterceptSchema, InterceptProp, RequiredPropByValidateTag to Reflector Configuration section with usage examples and option table
- Add new hooks to Features list

@llamapreview llamapreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI Code Review by LlamaPReview

🎯 TL;DR & Recommendation

Recommendation: Request Changes

This PR adds powerful schema/property interception hooks and a RequiredPropByValidateTag option, but introduces a critical breaking change in the format of uint/uint32 types and a duplicate required field bug that can produce invalid OpenAPI specs.

📄 Documentation Diagram

This diagram documents the new schema and property interception hooks flow.

sequenceDiagram
    participant Caller as Spec Router
    participant Reflector as Reflector
    participant SchemaHook as InterceptSchema
    participant PropHook as InterceptProp
    Caller->>Reflector: SchemaForType(t, mode, field)
    Reflector->>SchemaHook: Pre-call (Processed=false)
    alt stop==true
        SchemaHook-->>Reflector: Use provided schema<br/>Skip default processing
    else stop==false
        Reflector->>Reflector: Generate schema (inline/component)
        Reflector->>SchemaHook: Post-call (Processed=true)<br/>Modify final schema
    end
    Reflector->>PropHook: For each field: Pre-call (Processed=false)<br/>Return ErrSkipProperty to skip
    alt skipped
        PropHook-->>Reflector: Skip field
    else not skipped
        Reflector->>Reflector: Generate property schema
        Reflector->>PropHook: Post-call (Processed=true)<br/>Modify or skip property<br/>Note: RequiredPropByValidateTag uses post-hook
    end
    Reflector->>Caller: Return final schema
Loading

🌟 Strengths

  • Solid test coverage for new interception hooks, including pre/post call order and component scenarios.

⚡ Key Risks & Improvements (P1)

  • option/reflector.go: RequiredPropByValidateTag can add duplicate entries to the required array when combined with existing required struct tags, producing semantically invalid specs that may fail consumer validation.
  • internal/reflect/converter.go: Changing uint/uint32 format from int32 to int64 is a breaking change for downstream consumers; this should be documented as a major version change and a migration path considered.

📈 Risk Diagram

This diagram illustrates the risk of breaking existing consumers due to the uint/uint32 format change.

sequenceDiagram
    participant Upstream as Library vPrevious
    participant Consumer as Existing Clients
    participant Lib as Library vNew (PR)
    Consumer->>Upstream: Generate spec with uint32<br/>Expected format: int32
    Upstream-->>Consumer: uint32 format: int32
    note over Consumer,Upstream: R1(P1): uint/uint32 format changed<br/>from int32 to int64<br/>Schemas now emit format: int64
    Consumer->>Lib: Regenerate spec after upgrade
    Lib-->>Consumer: uint32 format: int64
    note over Consumer: Client stubs/code gen/validators<br/>may fail due to format mismatch
Loading

💡 Have feedback? We'd love to hear it in our GitHub Discussions.
✨ This review was generated by LlamaPReview Advanced, which is free for all open-source projects. Learn more.

Comment thread option/reflector.go
Comment thread internal/reflect/converter.go
@codecov

codecov Bot commented May 11, 2026

Copy link
Copy Markdown

akfaiz added 2 commits May 11, 2026 20:16
…opagation

- Chain InterceptSchema and InterceptProp hooks instead of overwriting previous ones
- Propagate errors from all hook calls through SchemaForType, StructSchema, RefSchema,
  SchemaForValue, RequestParts, ParameterSchema, AddRequest, and AddResponse
- Add pre-hook coverage for SchemaExposer types in both SchemaForType and SchemaForValue
- Clean up r.Components and r.Generating on pre-hook error in RefSchema to prevent
  stale empty components blocking retry
- Add uniqueStrings deduplication on Required to prevent duplicates when both
  required struct tag and RequiredPropByValidateTag hook are used
- Add parentSnapshot restore on ErrSkipProperty in post-hooks to roll back parent
  schema mutations (AllOf, AnyOf, OneOf, Extensions, Extra)
- Pass ParentType to InterceptPropParams for richer hook context
@akfaiz akfaiz merged commit 9d0fbbe into main May 11, 2026
7 of 8 checks passed
@akfaiz akfaiz deleted the feat/adjust-reflection branch May 11, 2026 15:27
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