redwoodjs / redwood

The App Framework for Startups
https://redwoodjs.com
MIT License
17.35k stars 999 forks source link

[Bug]: Service generation for a string array field, sets string value instead of array (enums and string[]) #6378

Open dthyresson opened 2 years ago

dthyresson commented 2 years ago

What's not working?

Service test and scenario generation for a string array field, sets string value instead of array for fields that are arrays of string and arrays of string enums.

How do we reproduce the bug?

Given the following model and enum in a Postgres database (for enum support),

enum EntityType {
  ATHLETE
  COMPANY
  CITY
  COUNTRY
  LEAGUE
  ORGANIZATION
  PLACE
  PERSON
  TEAM
  VENUE
}

model Entity {
  id                   String           @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  createdAt            DateTime         @default(now())
  updatedAt            DateTime         @updatedAt
  entityType           EntityType[]
...

and generating the sdl/service:

yarn rw g sdl Entity --force --crud

generates:

  scenario('creates a entity', async () => {
    const result = await createEntity({
      input: {
        updatedAt: '2022-09-11T14:40:53Z',
        entityType: 'ATHLETE',
        name: 'String',
        combinedStockSymbols: 'String',
        sports: 'ICE_HOCKEY',
        playerPositions: 'GOALKEEPER',
        venueTypes: 'ARENA',
      },
    })

where all the string enum arrays (aka multiple enum values):

image

are incorrect.

In fact, combinedStockSymbols is just combinedStockSymbols String[] and is also incorrect.

Setting to arrays of string, is valid:

image

Similar for scenarios:

image

What's your environment? (If it applies)

OSX
Postgres
v3-rc

Are you interested in working on this?

dthyresson commented 2 years ago

If might be in the services.js generator that scenarioFieldValue checks in an enum, set the first value when it should instead check if an enum and array, then set an array of first value:

    default: {
      if (field.kind === 'enum' && field.enumValues[0]) {
        return field.enumValues[0].dbName || field.enumValues[0].name
      }
    }
dthyresson commented 2 years ago

Will want to add tests for: