项目作者: thomasraynal

项目描述 :
Nancy + Swagger + Handlebars
高级语言: C#
项目地址: git://github.com/thomasraynal/SwaggerPlayground.git
创建时间: 2019-11-10T18:30:50Z
项目社区:https://github.com/thomasraynal/SwaggerPlayground

开源协议:

下载


SwaggerPlayground

Generating Nancy API from swagger specification using BeezUp dotnet-codegen and Handlebars.Net.

This an experimental code generator tool which allow the generation of the backbone of a service based on a swagger specification.

The idea is to generate:

  • The Nancy module (i.e controller) of the service, as an abstract class allowing customization of the route behavior (specifically security issues). It should handle exception mapping with HTTP status code, given the swagger response spec for that route as well as, of course, the response status code.
  • The request DTO with its validator (using FluentValidation), and default validation pattern given the swagger specs, both for the request and for the nested objects, recursively. Each validator being an extendable abstract class.
  • The response DTO (the first 2XX response found on the specification), which usually is a swagger definition, rendered as an extendable partial class.
  • The swagger definition objects, also with their validators, with a special attention given to the inlined object definition (rendered as ValueTuple) and enum.
  1. complexObjectE:
  2. type: object
  3. properties:
  4. pack_type:
  5. type: string
  6. enum:
  7. - nice
  8. - naughty
  9. - huge_bonus
  10. - immortal
  11. - stocking_killer
  12. team:
  13. type: string
  14. enum: [aog, au, afs, cgs, cpp, egc, fea, hl, sbr, uosp, vt]
  15. something:
  16. type: object
  17. additionalProperties:
  18. type: object
  19. additionalProperties:
  20. type: object
  21. properties:
  22. propA:
  23. type: integer
  24. format: int64
  25. propB:
  26. type: object
  27. properties:
  28. propC:
  29. type: integer
  30. format: int64
  31. propD:
  32. type: object
  33. additionalProperties:
  34. type: object
  35. additionalProperties:
  36. type: string
  37. format: date-time
  1. public partial class ComplexObjectE
  2. {
  3. public Pack_type Pack_type {get; set; }
  4. public Team Team {get; set; }
  5. public IEnumerable<(string, IEnumerable<(string, ( long propA , ( long propC , IEnumerable<(string, IEnumerable<(string, DateTime )> )> propD ) propB ))> )> Something {get; set; }
  6. }
  • The service interface, which, implemented, will send back to the controller the required response.

TL;DR:

  1. swagger: "2.0"
  2. info:
  3. description: "This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters"
  4. version: 1.0.0
  5. title: Swagger Petstore YAML
  6. termsOfService: "http://swagger.io/terms/"
  7. contact:
  8. email: "apiteam@swagger.io"
  9. license:
  10. name: Apache 2.0
  11. url: "http://www.apache.org/licenses/LICENSE-2.0.html"
  12. basePath: /v2
  13. x-product:
  14. appName: petStoreApp
  15. appService: petStore
  16. [...]
  17. paths:
  18. /pet:
  19. post:
  20. tags:
  21. - pet
  22. summary: Add a new pet to the store
  23. description: ""
  24. operationId: addPet
  25. consumes:
  26. - application/json
  27. - application/xml
  28. produces:
  29. - application/xml
  30. - application/json
  31. parameters:
  32. - in: body
  33. name: body
  34. description: Pet object that needs to be added to the store
  35. required: true
  36. schema:
  37. $ref: "#/definitions/Pet"
  38. responses:
  39. "201":
  40. schema:
  41. $ref: "#/definitions/Pet"
  42. description: successful operation
  43. "400":
  44. description: Invalid ID supplied
  45. "405":
  46. description: Invalid input
  47. [...]
  1. public interface IPetStoreService
  2. {
  3. Task< Pet > AddPet(AddPetRequest request);
  4. [...]
  5. }
  1. public abstract class PetStoreModuleBase : NancyModule
  2. {
  3. public PetStoreModuleBase(IPetStoreService petStoreService) : base("/v2")
  4. {
  5. Post("/pet", async (parameters, token) =>
  6. {
  7. ConfigureAddPetRequestRoute();
  8. return await this.EvaluateAndBind<AddPetRequest>(async (request) => await petStoreService.AddPet(request), HttpStatusCode.Created );
  9. });
  10. }
  11. [...]
  12. protected virtual void ConfigureAddPetRequestRoute()
  13. {
  14. }
  15. [...]
  16. }
  1. public partial class AddPetRequest
  2. {
  3. public Pet Body { get; set; }
  4. public override bool Equals(object obj)
  5. {
  6. return obj is AddPetRequest && obj.GetHashCode() == GetHashCode();
  7. }
  8. public override int GetHashCode()
  9. {
  10. unchecked
  11. {
  12. var hashCode = nameof(AddPetRequest).GetHashCode();
  13. if (default != Body) hashCode = (hashCode * 397) ^ Body.GetHashCode();
  14. return hashCode;
  15. }
  16. }
  17. }
  18. public class HttpResponseAddPet400Exception : Exception, IHasHttpServiceError
  19. {
  20. [...]
  21. }
  22. public class HttpResponseAddPet405Exception : Exception, IHasHttpServiceError
  23. {
  24. [...]
  25. }
  26. public class AddPetRequestValidator : AbstractValidator<AddPetRequest>
  27. {
  28. public AddPetRequestValidator()
  29. {
  30. RuleFor(request => request.Body).NotNull().WithMessage("Body is required");
  31. }
  32. }
  33. }
  1. definitions:
  2. Pet:
  3. required:
  4. - name
  5. - photoUrls
  6. properties:
  7. id:
  8. type: integer
  9. format: int64
  10. category:
  11. $ref: "#/definitions/Category"
  12. name:
  13. type: string
  14. example: doggie
  15. photoUrls:
  16. type: array
  17. xml:
  18. name: photoUrl
  19. wrapped: true
  20. items:
  21. type: string
  22. tags:
  23. type: array
  24. xml:
  25. name: tag
  26. wrapped: true
  27. items:
  28. $ref: "#/definitions/Tag"
  29. petStatus:
  30. type: string
  31. description: pet status in the store
  32. enum:
  33. - available
  34. - pending
  35. - sold
  36. xml:
  37. name: Pet
  1. public partial class Pet
  2. {
  3. public long Id {get; set; }
  4. public Category Category {get; set; }
  5. public string Name {get; set; }
  6. public IEnumerable< string > PhotoUrls {get; set; }
  7. public IEnumerable<Tag> Tags {get; set; }
  8. public PetStatus PetStatus {get; set; }
  9. }
  10. public abstract class PetValidatorBase : AbstractValidator<Pet>
  11. {
  12. public PetValidatorBase()
  13. {
  14. RuleFor(dto => dto.Name).NotNull().NotEmpty().WithMessage("Name is required");
  15. RuleFor(dto => dto.PhotoUrls).NotNull().WithMessage("PhotoUrls is required");
  16. }
  17. }