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+ OperationDefinition (
37+ OpenAPI openAPI ,
38+ Operation operation ,
39+ String path ,
40+ String method ,
41+ boolean emulateSwaggerV2BodyParameters ) {
3642 this .openAPI = openAPI ;
3743 this .operation = operation ;
3844 this .path = path ;
3945 this .method = method ;
46+ this .emulateSwaggerV2BodyParameters = emulateSwaggerV2BodyParameters ;
4047 }
4148
4249 String getMethod () {
@@ -52,67 +59,66 @@ Operation getOperation() {
5259 }
5360
5461 List <String > getServers () {
62+ if (openAPI .getServers () == null ) {
63+ return List .of ();
64+ }
5565 return openAPI .getServers ().stream ().map (Server ::getUrl ).toList ();
5666 }
5767
58- List <Parameter > getParameters () {
68+ List <ParameterDefinition > getParameters () {
69+ return emulateSwaggerV2BodyParameters ? getSwaggerV2Parameters () : getOpenApiParameters ();
70+ }
71+
72+ private List <ParameterDefinition > getOpenApiParameters () {
5973 if (operation .getParameters () == null ) {
6074 return List .of ();
6175 }
62- return operation .getParameters ();
76+ return operation .getParameters (). stream (). map ( ParameterDefinition :: new ). toList () ;
6377 }
6478
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- }
79+ @ SuppressWarnings ({"rawtypes" })
80+ private List <ParameterDefinition > getSwaggerV2Parameters () {
81+ if (operation .getParameters () != null && !operation .getParameters ().isEmpty ()) {
82+ return operation .getParameters ().stream ().map (ParameterDefinition ::new ).toList ();
8383 }
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- }
84+ if (operation .getRequestBody () != null ) {
85+ Schema <?> schema = null ;
86+ if (operation .getRequestBody ().getContent () != null
87+ && operation
88+ .getRequestBody ()
89+ .getContent ()
90+ .containsKey (jakarta .ws .rs .core .MediaType .APPLICATION_JSON )) {
91+ MediaType mt =
92+ operation
93+ .getRequestBody ()
94+ .getContent ()
95+ .get (jakarta .ws .rs .core .MediaType .APPLICATION_JSON );
96+ schema = mt .getSchema ();
97+ } else if (operation .getRequestBody ().get$ref () != null ) {
98+ schema = resolveSchema (operation .getRequestBody ().get$ref ());
9699 }
97- }
98100
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- }
101+ if (schema == null ) {
102+ return List .of ();
105103 }
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 ();
104+
105+ Set <String > required =
106+ schema .getRequired () != null ? new HashSet <>(schema .getRequired ()) : new HashSet <>();
107+
108+ Map <String , Schema > properties = schema .getProperties ();
109+ if (properties != null ) {
110+ List <ParameterDefinition > result = new ArrayList <>();
111+ for (Map .Entry <String , Schema > prop : properties .entrySet ()) {
112+ String fieldName = prop .getKey ();
113+ ParameterDefinition fieldParam =
114+ new ParameterDefinition (
115+ fieldName , "body" , required .contains (fieldName ), prop .getValue ());
116+ result .add (fieldParam );
110117 }
118+ return result ;
111119 }
112120 }
113-
114- throw new IllegalStateException (
115- "No content type found for operation " + operation .getOperationId () + " [" + method + "]" );
121+ return List .of ();
116122 }
117123
118124 Schema <?> resolveSchema (String ref ) {
0 commit comments