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

"Invalid Class Typecast" Error with Active Record and Enumerated Type #583

Closed
marcelojaloto opened this issue Oct 21, 2022 · 1 comment
Closed

Comments

@marcelojaloto
Copy link
Contributor

marcelojaloto commented Oct 21, 2022

1- When you use Active Record and need to insert an enumerated field type then the error below occurs:


Debugger Exception Notification

Project Server.exe raised exception class EInvalidCast with message 'Invalid class typecast'.

The solution for this problem you need to change Ord(aValue.AsInteger) to aValue.AsOrdinal.

Unit: MVCFramework.ActiveRecord
Method: MapTValueToParam
Class: TMVCActiveRecord
Line: 2013

    tkEnumeration:
      begin
        if aValue.TypeInfo = TypeInfo(System.Boolean) then
        begin
          aParam.AsBoolean := aValue.AsBoolean;
        end
        else
        begin
          aParam.AsInteger := aValue.AsOrdinal;
        end;
      end;

2- Another error occurs when you use Active Record and you need to get an enumerated type field from the database.


Debugger Exception Notification

Project Server.exe raised exception class EInvalidCast with message 'Invalid class typecast'.

type

  TThemes = (Light, Dark);

  [MVCNameCase(ncLowerCase)]
  [MVCTable('sample')]
  TSample = class(TMVCActiveRecord)
  strict protected
    [MVCTableField('theme')]
    FTheme: TThemes;
  public
    [MVCEnumSerialization(estEnumName)]
    [MVCSwagJsonSchemaField(stEnumeration, 'theme', 'Theme', True, False, 0, 1)]
    property Theme: TThemes read FTheme write FTheme;
  end;
TSamples = class(TObjectList<TSample>)
public
  class function ReturnItem: TSample;
end;

implementation

class function TSamples.ReturnItem: TSample;
begin
  Result := TModel.GetFirstByWhere<TSample>('', [], [], False);
end;

The solution for this problem you need include a new setting when field type is a Enumeration.

Unit: MVCFramework.Serializer.Commons
Line: 1115
Method: MapDataSetFieldToRTTIField
New Lines:

        else if (aRTTIField.FieldType.TypeKind = tkEnumeration) then
        begin
          TValue(AField.AsInteger).ExtractRawData(PByte(Pointer(AObject)) + aRTTIField.Offset);
        end
procedure MapDataSetFieldToRTTIField(const AField: TField; const aRTTIField: TRttiField;
const AObject: TObject);
...
begin
  ...
  case AField.DataType of
  ...
    ftInteger, ftSmallint, ftShortint, ftByte:
      begin
        ...
        else if (aRTTIField.FieldType.TypeKind = tkEnumeration) then
        begin
          TValue(AField.AsInteger).ExtractRawData(PByte(Pointer(AObject)) + aRTTIField.Offset);
        end
      end
  ...
  end;
end;
@marcelojaloto
Copy link
Contributor Author

@danieleteti Can you accept my Pull Request?

#584

marcelojaloto added a commit to marcelojaloto/delphimvcframework that referenced this issue Oct 21, 2022
danieleteti#583 - Fixes bugs 'Invalid class typecast' when using Active Record and Enumerated Type
danieleteti pushed a commit that referenced this issue Oct 21, 2022
…ord and Enumerated Type (#584)

* Issue #546 was missing from the merge.

* #583 - Fixes bugs 'Invalid class typecast' when using Active Record and Enumerated Type;
danieleteti pushed a commit that referenced this issue Oct 18, 2024
* Issue #546 was missing from the merge.

* #581 - Fix error on removing default connection;

* #583 - Fixes bugs 'Invalid class typecast' when using Active Record and Enumerated Type;

* issue #789;

* issue #789;
danieleteti pushed a commit that referenced this issue Nov 29, 2024
* Issue #546 was missing from the merge.

* #581 - Fix error on removing default connection;

* #583 - Fixes bugs 'Invalid class typecast' when using Active Record and Enumerated Type;
danieleteti pushed a commit that referenced this issue Jan 6, 2025
* Issue #546 was missing from the merge.

* #581 - Fix error on removing default connection;

* #583 - Fixes bugs 'Invalid class typecast' when using Active Record and Enumerated Type;

* Fixes rendering in Swagger documentation for TArray type fields.

* It also allows overriding the MapTValueToParam method of the TMVCActiveRecord class.
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

No branches or pull requests

2 participants