33import io .swagger .v3 .oas .annotations .Hidden ;
44import io .swagger .v3 .oas .models .Components ;
55import io .swagger .v3 .oas .models .OpenAPI ;
6+ import io .swagger .v3 .oas .models .Operation ;
67import io .swagger .v3 .oas .models .media .ArraySchema ;
78import io .swagger .v3 .oas .models .media .Content ;
89import io .swagger .v3 .oas .models .media .MapSchema ;
910import io .swagger .v3 .oas .models .media .MediaType ;
1011import io .swagger .v3 .oas .models .media .ObjectSchema ;
1112import io .swagger .v3 .oas .models .media .Schema ;
13+ import io .swagger .v3 .oas .models .parameters .RequestBody ;
1214
1315import javax .lang .model .element .AnnotationMirror ;
1416import javax .lang .model .element .Element ;
3335 */
3436public class SchemaBuilder {
3537
38+ private static final String APP_FORM = "application/x-www-form-urlencoded" ;
39+ private static final String APP_JSON = "application/json" ;
40+
3641 private final Types types ;
3742 private final KnownTypes knownTypes ;
3843 private final TypeMirror iterableType ;
@@ -59,6 +64,61 @@ public Content createContent(TypeMirror returnType, String mediaType) {
5964 return content ;
6065 }
6166
67+ /**
68+ * Add parameter as a form parameter.
69+ */
70+ public void addFormParam (Operation operation , String varName , Schema schema ) {
71+ RequestBody body = requestBody (operation );
72+ Schema formSchema = requestFormParamSchema (body );
73+ formSchema .addProperties (varName , schema );
74+ }
75+
76+ private Schema requestFormParamSchema (RequestBody body ) {
77+
78+ final Content content = body .getContent ();
79+ MediaType mediaType = content .get (APP_FORM );
80+
81+ Schema schema ;
82+ if (mediaType != null ) {
83+ schema = mediaType .getSchema ();
84+ } else {
85+ schema = new Schema ();
86+ schema .setType ("object" );
87+ mediaType = new MediaType ();
88+ mediaType .schema (schema );
89+ content .addMediaType (APP_FORM , mediaType );
90+ }
91+ return schema ;
92+ }
93+
94+ /**
95+ * Add as request body.
96+ */
97+ public void addRequestBody (Operation operation , Schema schema , boolean asForm , String description ) {
98+
99+ RequestBody body = requestBody (operation );
100+ body .setDescription (description );
101+
102+ MediaType mt = new MediaType ();
103+ mt .schema (schema );
104+
105+ String mime = asForm ? APP_FORM : APP_JSON ;
106+ body .getContent ().addMediaType (mime , mt );
107+ }
108+
109+ private RequestBody requestBody (Operation operation ) {
110+
111+ RequestBody body = operation .getRequestBody ();
112+ if (body == null ) {
113+ body = new RequestBody ();
114+ body .setRequired (true );
115+ Content content = new Content ();
116+ body .setContent (content );
117+ operation .setRequestBody (body );
118+ }
119+ return body ;
120+ }
121+
62122 public Schema <?> toSchema (TypeMirror type ) {
63123
64124 Schema <?> schema = knownTypes .createSchema (type .toString ());
0 commit comments