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

Support for FormArray #28

Open
tommie822 opened this issue Mar 9, 2022 · 9 comments
Open

Support for FormArray #28

tommie822 opened this issue Mar 9, 2022 · 9 comments
Labels
enhancement New feature or request

Comments

@tommie822
Copy link

tommie822 commented Mar 9, 2022

It would be nice to let Open Api Specification arrays map to the Angular FormArray.
As it makes creating forms with arrays more easier.

I would like to implement it but i'm curious if you have a specific reason why it isn't currently implemented.

@martinmcwhorter
Copy link
Contributor

I am happy to help craft a PR for this.

Can you provide me an example open-api doc and what you would expect the form to look like for this?

@martinmcwhorter martinmcwhorter added the enhancement New feature or request label Jun 1, 2022
@Arberto99
Copy link

Thanks a lot! It would be amazing!

For this open-api doc:

"ReadResourceDto": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "NestedResource": {
            "$ref": "#/components/schemas/NestedResourceDto"
          },
          "NestedResourceArray": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/NestedResourceArrayDto"
            }
          },
        },
        "required": [
          "id",
          "name",
          "NestedResource",
          "NestedResourceArray",
        ]
},

"NestedResourceArrayDto": {
    "type": "object",
    "properties": {
        "id": {
           "type": "string",
           "format": "uuid"
         },
         "name": {
           "type": "string"
          }
    },
    "required": [
          "id",
          "name"
    ]
},
      
"NestedResourceDto": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "name"
        ]
},

Right now, the generated code is this:

export const ReadResourceDtoForm = new FormGroup({
  id: new FormControl(null, [Validators.required]),
  name: new FormControl(null, [Validators.required]),
  description: new FormControl(null),
  NestedResource: new FormControl(null, [Validators.required]),
  NestedResourceArray: new FormControl(null, [Validators.required]),
});

But it would be expected something like this:

export const readApplicationDtoForm = new FormGroup({
  id: new FormControl(null, [Validators.required]),
  name: new FormControl(null, [Validators.required]),
  description: new FormControl(null),
  NestedResource: new FormGroup({
      id: new FormControl(null, [Validators.required]),
      name: new FormControl(null, [Validators.required]),
  }),
  NestedResourceArray: new FormArray(
  ...
  ),
});

If you need any help for development I'll be glad to help you.

Thanks a lot!

@RoRep1ay
Copy link

Hey @Arberto99 , I did a bit drafting related to supporting form array and nested form group, though I'm not quite sure how we construct FormArray in this case.

From your API spec:

          "NestedResourceArray": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/NestedResourceArrayDto"
            }
          },

What would be expected result here?

  NestedResourceArray: new FormArray(
  ...
  ),

@Arberto99
Copy link

Hi @RoRep1ay ! Thanks for your answer!

The result should be something like this:

NestedResourceArray: new FormArray([
    new FormGroup(
          id: new FormControl(null, [Validators.required]),
          name: new FormControl(null, [Validators.required]),
    )
])

What do you think about it?
It sounds good to me, but I'm ready to listen some alternative solutions.

Thanks again anyway!

@RoRep1ay
Copy link

Hi @Arberto99 ,

I'm OK with your proposal, though there is still one small details that we haven't discussed yet, which is related to minItems and maxItems. What's your opinion to it?

To me, I prefer to ignore maxItems spec, for minItems, we generate formarray with array values the same as the minItems value.

Ex:

definitions:
  FooModel:
    type: object
    properties:
      foo:
        type: array
        minItems: 3
        maxItems: 10
        items:
          type: string
          minLength: 1

Output:

foo = new FormArray([
  new FormControl(null, Validators.minLength(1))
  new FormControl(null, Validators.minLength(1))
  new FormControl(null, Validators.minLength(1))
])

@Arberto99
Copy link

Yes, I'm ok with it.

@martinmcwhorter
Copy link
Contributor

Alright. This is really becoming clearer to me now on how this can be done. Discussion like this really does help.

@RoRep1ay
Copy link

@martinmcwhorter , if you're OK with the spec now, may I take over this issue?

@martinmcwhorter
Copy link
Contributor

Absolutely. Take a stab at it and make a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants