Search code examples
angularspring-bootswagger-codegen

Swagger codgen: How to add additional attributes in swagger codgen


I have been using Swagger Codegen to generate models from the backend to frontend (Spring Boot -> Angular) and I don't know how to add additional attributes automatically to generated frontend models. Below is an example, the frontend model has additional attribute "Mode[]"

For example:

  • backend model:
    @Entity
    @NoArgsConstructor
    @Getter
    @Setter
    public class Project {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id_project")
        private Long idProject;
    
        private String name;
    
        @JsonBackReference
        @OneToMany(mappedBy = "project")
        private List<Subproject> subprojects;
        
        @ManyToOne
        @ApiModelProperty(dataType = "long")
        @JoinColumn(name = "client_id_client")
        private Client client;
    
    }*
  • frontend model:
    export interface Project {
      idProject: number;
      name: string;
      client: Client;
      **modes: Mode[];**
      subprojects: Subproject[];
    }

Thank you!


Solution

  • it just doesn't allow that and it shouldn't. it was made to make it easy to consume your API. if you need additional fields I would recommend you to make extension of the interface that you got from codegen.

     export interface ProjectData extends Project {
       modes: Mode[]:
     }
    

    it would even be a stricter typings for you app as you would have separate types for just what you get from API.

    http.get<Project>(url).pipe(map(enrichWithModes))
    ...
    enrichWithModes(project: Project): ProjectData {
      // ....
    return {...project, modes: []}
    }