1717
1818import io .swagger .v3 .oas .models .OpenAPI ;
1919import io .swagger .v3 .oas .models .Operation ;
20- import io .swagger .v3 .oas .models .media .Content ;
2120import io .swagger .v3 .oas .models .media .MediaType ;
2221import io .swagger .v3 .oas .models .media .Schema ;
23- import io .swagger .v3 .oas .models .parameters .Parameter ;
24- import io .swagger .v3 .oas .models .responses .ApiResponse ;
2522import io .swagger .v3 .oas .models .servers .Server ;
23+ import java .util .ArrayList ;
24+ import java .util .HashSet ;
2625import java .util .List ;
2726import java .util .Map ;
27+ import java .util .Set ;
2828
2929class OperationDefinition {
3030 private final Operation operation ;
3131 private final String method ;
3232 private final OpenAPI openAPI ;
3333 private final String path ;
34+ private final boolean emulateSwaggerV2BodyParameters ;
3435
35- OperationDefinition (OpenAPI openAPI , Operation operation , String path , String method ) {
36+ private static final String APPLICATION_JSON = "application/json" ;
37+
38+ OperationDefinition (
39+ OpenAPI openAPI ,
40+ Operation operation ,
41+ String path ,
42+ String method ,
43+ boolean emulateSwaggerV2BodyParameters ) {
3644 this .openAPI = openAPI ;
3745 this .operation = operation ;
3846 this .path = path ;
3947 this .method = method ;
48+ this .emulateSwaggerV2BodyParameters = emulateSwaggerV2BodyParameters ;
4049 }
4150
4251 String getMethod () {
@@ -47,72 +56,59 @@ String getPath() {
4756 return path ;
4857 }
4958
50- Operation getOperation () {
51- return operation ;
52- }
53-
5459 List <String > getServers () {
60+ if (openAPI .getServers () == null ) {
61+ return List .of ();
62+ }
5563 return openAPI .getServers ().stream ().map (Server ::getUrl ).toList ();
5664 }
5765
58- List <Parameter > getParameters () {
66+ List <ParameterDefinition > getParameters () {
67+ return emulateSwaggerV2BodyParameters ? getSwaggerV2Parameters () : getOpenApiParameters ();
68+ }
69+
70+ private List <ParameterDefinition > getOpenApiParameters () {
5971 if (operation .getParameters () == null ) {
6072 return List .of ();
6173 }
62- return operation .getParameters ();
74+ return operation .getParameters (). stream (). map ( ParameterDefinition :: new ). toList () ;
6375 }
6476
65- @ SuppressWarnings ({"rawtypes" , "unchecked" })
66- Map <String , Schema > getBody () {
67- if (operation .getRequestBody () != null && operation .getRequestBody ().getContent () != null ) {
68- Content content = operation .getRequestBody ().getContent ();
69- if (content .containsKey ("application/json" )) {
70- MediaType mt = content .get ("application/json" );
71- if (mt .getSchema ().get$ref () != null && !mt .getSchema ().get$ref ().isEmpty ()) {
72- Schema <?> schema = resolveSchema (mt .getSchema ().get$ref ());
73- return schema .getProperties ();
74- } else if (mt .getSchema ().getProperties () != null ) {
75- return mt .getSchema ().getProperties ();
76- } else {
77- throw new IllegalArgumentException (
78- "Can't resolve schema for request body of operation " + operation .getOperationId ());
79- }
80- } else {
81- throw new IllegalArgumentException ("Only 'application/json' content type is supported" );
82- }
77+ @ SuppressWarnings ({"rawtypes" })
78+ private List <ParameterDefinition > getSwaggerV2Parameters () {
79+ if (operation .getParameters () != null && !operation .getParameters ().isEmpty ()) {
80+ return operation .getParameters ().stream ().map (ParameterDefinition ::new ).toList ();
8381 }
84- return Map .of ();
85- }
86-
87- String getContentType () {
88- String method = getMethod ().toUpperCase ();
89-
90- if (method .equals ("POST" ) || method .equals ("PUT" ) || method .equals ("PATCH" )) {
91- if (operation .getRequestBody () != null && operation .getRequestBody ().getContent () != null ) {
92- Content content = operation .getRequestBody ().getContent ();
93- if (!content .isEmpty ()) {
94- return content .keySet ().iterator ().next ();
95- }
82+ if (operation .getRequestBody () != null ) {
83+ Schema <?> schema = null ;
84+ if (operation .getRequestBody ().getContent () != null
85+ && operation .getRequestBody ().getContent ().containsKey (APPLICATION_JSON )) {
86+ MediaType mt = operation .getRequestBody ().getContent ().get (APPLICATION_JSON );
87+ schema = mt .getSchema ();
88+ } else if (operation .getRequestBody ().get$ref () != null ) {
89+ schema = resolveSchema (operation .getRequestBody ().get$ref ());
9690 }
97- }
9891
99- if (operation .getResponses () != null ) {
100- for (String code : new String [] {"200" , "201" , "204" }) {
101- ApiResponse resp = operation .getResponses ().get (code );
102- if (resp != null && resp .getContent () != null && !resp .getContent ().isEmpty ()) {
103- return resp .getContent ().keySet ().iterator ().next ();
104- }
92+ if (schema == null ) {
93+ return List .of ();
10594 }
106- for (Map .Entry <String , ApiResponse > e : operation .getResponses ().entrySet ()) {
107- Content content = e .getValue ().getContent ();
108- if (content != null && !content .isEmpty ()) {
109- return content .keySet ().iterator ().next ();
95+
96+ Set <String > required =
97+ schema .getRequired () != null ? new HashSet <>(schema .getRequired ()) : new HashSet <>();
98+
99+ Map <String , Schema > properties = schema .getProperties ();
100+ if (properties != null ) {
101+ List <ParameterDefinition > result = new ArrayList <>();
102+ for (Map .Entry <String , Schema > prop : properties .entrySet ()) {
103+ String fieldName = prop .getKey ();
104+ ParameterDefinition fieldParam =
105+ new ParameterDefinition (fieldName , "body" , required .contains (fieldName ));
106+ result .add (fieldParam );
110107 }
108+ return result ;
111109 }
112110 }
113-
114- throw new IllegalStateException (
115- "No content type found for operation " + operation .getOperationId () + " [" + method + "]" );
111+ return List .of ();
116112 }
117113
118114 Schema <?> resolveSchema (String ref ) {
0 commit comments