Xtroce
Xtroce

Reputation: 1824

where to put code for swagger generated server code

I am just starting with swagger to generate a java Spring API. The generation itself poses no problem. I used the PetStore yaml to generate the SpringBoot serverside code without a problem.

After i generate the code however I cannot find a good tutorial/way explaining to me where to best put the custom code i want to write. Writing it directly into the generated code does not seem like a good idea, since if i ever regenerate because of changes in the definitions you don't want to override the code used.

You get stubs like:

@Controller
public class PetApiController implements PetApi {
...
  public ResponseEntity<ModelApiResponse> uploadFile(@ApiParam(value = "ID of pet to update",required=true ) @PathVariable("petId") Long petId,
    @ApiParam(value = "Additional data to pass to server") @RequestPart(value="additionalMetadata", required=false)  String additionalMetadata,
      @ApiParam(value = "file detail") @RequestPart("file") MultipartFile file) {
      // do some magic!
      return new ResponseEntity<ModelApiResponse>(HttpStatus.OK);
  }
...
}

Now is there a way to fill the "do some magic!" part during/after generation with i.e. a call to a service, so that i can use this as sort of entry point. e.g. autowiring a service which has the same methods but can be provided by me, so that i can keep my implementations separate from the generated code.

@Controller
public class PetApiController implements PetApi {
...
@Autowired
PetApiService petApiService;
...
  public ResponseEntity<ModelApiResponse> uploadFile(@ApiParam(value = "ID of pet to update",required=true ) @PathVariable("petId") Long petId,
    @ApiParam(value = "Additional data to pass to server") @RequestPart(value="additionalMetadata", required=false)  String additionalMetadata,
      @ApiParam(value = "file detail") @RequestPart("file") MultipartFile file) {
      return petApiService.uploadFile(file);
  }
...
}

Thanks

Upvotes: 1

Views: 648

Answers (1)

Daniel Diment
Daniel Diment

Reputation: 110

There is no easy way of regenerating new code using Swagger and stop it from overwriting any code you want to make. Although there is a way of mitigate all the copy-pasting if you want to change the api. Instead of writing all the code on the "do some magic" comment try to put in on a method in an outside class, so in the case you need to regenerate the code with swagger, you would only need to copy a single line, I'll give you an example:

@Controller
public class PetApiController implements PetApi {

@Autowired
GenerateResponse generateResponse;
...
  public ResponseEntity<ModelApiResponse> uploadFile(@ApiParam(value = "ID of pet to update",required=true ) @PathVariable("petId") Long petId,
    @ApiParam(value = "Additional data to pass to server") @RequestPart(value="additionalMetadata", required=false)  String additionalMetadata,
      @ApiParam(value = "file detail") @RequestPart("file") MultipartFile file) {
      return generateResponse.uploadFile(petId, additionalMetadata, file);
  }
...
}

And in your outside class that I called "GenerateResponse":

@Component
public class GenerateResponse{
@Autowired
PetApiService petApiService;

    public ResponseEntity<ModelApiResponse> uploadFile(Long petId, String additionalMetadata, MultipartFile file){
    return petApiService.uploadFile(file);
    }
}

So you just have to copy the "return GenerateResponse.uploadFile(petId, additionalMetadata, file);" line and only create the autowired GenerateResponse once every time you change it.

Upvotes: 1

Related Questions