blob: 80a32c242e678e98f761e13966a529a1278a640c [file] [log] [blame]
<!--
/***************************************************************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
***************************************************************************************************************************/
-->
OpenAPI Schema Part Parsing
<p>
Parameters annotated with any of the following are parsed using the registered {@link oaj.oapi.OpenApiParser} and
therefore support OpenAPI syntax and validation:
</p>
<ul class='javatree'>
<li class='ja'>{@link oaj.http.annotation.Header}
<li class='ja'>{@link oaj.http.annotation.Query}
<li class='ja'>{@link oaj.http.annotation.FormData}
<li class='ja'>{@link oaj.http.annotation.Path}
<li class='ja'>{@link oaj.http.annotation.Body} (<c>Content-Type</c> must match <js>"text/openapi"</js>)
</ul>
<p>
For example, the following shows how a pipe-delimited list of comma-delimited numbers (e.g. <js>"1,2,3|4,5,6|7,8,9"</js>) in a query parameter can be converted to a 2-dimensional array of <c>Longs</c>:
</p>
<p class='bpcode w800'>
<ja>@RestMethod</ja>(method=<js>"GET"</js>, path=<js>"/testQuery"</js>)
<jk>public void</jk> testQuery(
<ja>@Query</ja>(
name=<js>"queryParamName"</js>,
collectionFormat=<js>"pipes"</js>,
items=<ja>@SubItems</ja>(
collectionFormat=<js>"csv"</js>,
type=<js>"integer"</js>,
format=<js>"int64"</js>,
minimum=<js>"0"</js>,
maximum=<js>"100"</js>
minLength=1,
maxLength=10
),
minLength=1,
maxLength=10
)
Long[][] queryParameter
) {...}
</p>
<p>
Input will be converted based on the types and formats defined in the schema definition.
Input validations such as <c>minLength/maxLength</c> that don't match the input will result in automatic <c>400 Bad Request</c> responses.
</p>
<p>
The following shows the same for a request body:
</p>
<p class='bpcode w800'>
<ja>@RestMethod</ja>(method=<js>"POST"</js>, path=<js>"/testBody"</js>)
<jk>public void</jk> testBody(
<ja>@Body</ja>(
parsers=OpenApiParser.<jk>class</jk>,
defaultContentType=<js>"text/openapi"</js>,
schema=<ja>@Schema</ja>(
items=<ja>@Items</ja>(
collectionFormat=<js>"pipes"</js>,
items=<ja>@SubItems</ja>(
collectionFormat=<js>"csv"</js>,
type=<js>"integer"</js>,
format=<js>"int64"</js>,
minimum=<js>"0"</js>,
maximum=<js>"100"</js>
minLength=1,
maxLength=10
)
),
minLength=1,
maxLength=10
)
)
Long[][] body
) {...}
</p>
<p>
The list of valid POJO types for parameters depends on type and format of the value or items/entries of the value.
For example, instead of <c>Longs</c> in the example above, we could also define a 2-dimensional array of POJOs convertible from <c>Longs</c>:
</p>
<p class='bpcode w800'>
<ja>@RestMethod</ja>(method=<js>"POST"</js>, path=<js>"/2dLongArray"</js>)
<jk>public void</jk> testBody(<ja>@Body</ja>(...) MyPojo[][] body) {...}
<jc>// POJO convertible from a Long.</jc>
<jk>public class</jk> MyPojo {
<jk>public</jk> MyPojo(Long input) {...}
}
</p>
<p>
Or even POJOs that take in arrays of <c>Longs[]</c>:
</p>
<p class='bpcode w800'>
<ja>@RestMethod</ja>(method=<js>"POST"</js>, path=<js>"/2dLongArray"</js>)
<jk>public void</jk> testBody(<ja>@Body</ja>(...) MyPojo[] body) {...}
<jc>// POJO convertible from a Long[].</jc>
<jk>public class</jk> MyPojo {
<jk>public</jk> MyPojo(Long[] input) {...}
}
</p>
<p>
Or even POJOs that take in the whole 2-dimensional array:
</p>
<p class='bpcode w800'>
<ja>@RestMethod</ja>(method=<js>"POST"</js>, path=<js>"/2dLongArray"</js>)
<jk>public void</jk> testBody(<ja>@Body</ja>(...) MyPojo body) {...}
<jc>// POJO convertible from a Long[][].</jc>
<jk>public class</jk> MyPojo {
<jk>public</jk> MyPojo(Long[][] input) {...}
}
</p>
<p>
As you can see, the complexity of possible input types expands significantly.
For more information about valid parameter types, see {@doc juneau-marshall.OpenApiDetails.Parsers OpenAPI Parsers}
</p>