use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.
the class OpenAPI3_1SerializationTest method testDiscriminatorSerialization.
@Test
public void testDiscriminatorSerialization() {
Schema<String> propertySchema1 = new StringSchema();
propertySchema1.addType(propertySchema1.getType());
Schema<String> propertySchema2 = new StringSchema();
propertySchema2.addType(propertySchema2.getType());
Discriminator discriminator = new Discriminator().propertyName("type");
discriminator.addExtension("x-otherName", "discriminationType");
Schema schema = new ObjectSchema().addProperties("name", propertySchema1).addProperties("type", propertySchema1).discriminator(discriminator);
schema.addType(schema.getType());
OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("pet", schema));
SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + " schemas:\n" + " pet:\n" + " properties:\n" + " name:\n" + " type: string\n" + " type:\n" + " type: string\n" + " discriminator:\n" + " propertyName: type\n" + " x-otherName: discriminationType\n" + " type: object");
SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + " \"openapi\" : \"3.1.0\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"pet\" : {\n" + " \"properties\" : {\n" + " \"name\" : {\n" + " \"type\" : \"string\"\n" + " },\n" + " \"type\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " },\n" + " \"discriminator\" : {\n" + " \"propertyName\" : \"type\",\n" + " \"x-otherName\" : \"discriminationType\"\n" + " },\n" + " \"type\" : \"object\"\n" + " }\n" + " }\n" + " }\n" + "}");
openAPI.openapi("3.0.3");
SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + " schemas:\n" + " pet:\n" + " properties:\n" + " name:\n" + " type: string\n" + " type:\n" + " type: string\n" + " discriminator:\n" + " propertyName: type\n" + " type: object");
SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + " \"openapi\" : \"3.0.3\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"pet\" : {\n" + " \"properties\" : {\n" + " \"name\" : {\n" + " \"type\" : \"string\"\n" + " },\n" + " \"type\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " },\n" + " \"discriminator\" : {\n" + " \"propertyName\" : \"type\"\n" + " },\n" + " \"type\" : \"object\"\n" + " }\n" + " }\n" + " }\n" + "}");
}
use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.
the class Reader method read.
public OpenAPI read(Class<?> cls, String parentPath, String parentMethod, boolean isSubresource, RequestBody parentRequestBody, ApiResponses parentResponses, Set<String> parentTags, List<Parameter> parentParameters, Set<Class<?>> scannedResources) {
Hidden hidden = cls.getAnnotation(Hidden.class);
// class path
final javax.ws.rs.Path apiPath = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Path.class);
if (hidden != null) {
// || (apiPath == null && !isSubresource)) {
return openAPI;
}
io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.responses.ApiResponse.class);
List<io.swagger.v3.oas.annotations.security.SecurityScheme> apiSecurityScheme = ReflectionUtils.getRepeatableAnnotations(cls, io.swagger.v3.oas.annotations.security.SecurityScheme.class);
List<io.swagger.v3.oas.annotations.security.SecurityRequirement> apiSecurityRequirements = ReflectionUtils.getRepeatableAnnotations(cls, io.swagger.v3.oas.annotations.security.SecurityRequirement.class);
ExternalDocumentation apiExternalDocs = ReflectionUtils.getAnnotation(cls, ExternalDocumentation.class);
io.swagger.v3.oas.annotations.tags.Tag[] apiTags = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.tags.Tag.class);
io.swagger.v3.oas.annotations.servers.Server[] apiServers = ReflectionUtils.getRepeatableAnnotationsArray(cls, io.swagger.v3.oas.annotations.servers.Server.class);
javax.ws.rs.Consumes classConsumes = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Consumes.class);
javax.ws.rs.Produces classProduces = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Produces.class);
boolean classDeprecated = ReflectionUtils.getAnnotation(cls, Deprecated.class) != null;
// OpenApiDefinition
OpenAPIDefinition openAPIDefinition = ReflectionUtils.getAnnotation(cls, OpenAPIDefinition.class);
if (openAPIDefinition != null) {
// info
AnnotationsUtils.getInfo(openAPIDefinition.info()).ifPresent(info -> openAPI.setInfo(info));
// OpenApiDefinition security requirements
SecurityParser.getSecurityRequirements(openAPIDefinition.security()).ifPresent(s -> openAPI.setSecurity(s));
//
// OpenApiDefinition external docs
AnnotationsUtils.getExternalDocumentation(openAPIDefinition.externalDocs()).ifPresent(docs -> openAPI.setExternalDocs(docs));
// OpenApiDefinition tags
AnnotationsUtils.getTags(openAPIDefinition.tags(), false).ifPresent(tags -> openApiTags.addAll(tags));
// OpenApiDefinition servers
AnnotationsUtils.getServers(openAPIDefinition.servers()).ifPresent(servers -> openAPI.setServers(servers));
// OpenApiDefinition extensions
if (openAPIDefinition.extensions().length > 0) {
openAPI.setExtensions(AnnotationsUtils.getExtensions(openAPIDefinition.extensions()));
}
}
// class security schemes
if (apiSecurityScheme != null) {
for (io.swagger.v3.oas.annotations.security.SecurityScheme securitySchemeAnnotation : apiSecurityScheme) {
Optional<SecurityParser.SecuritySchemePair> securityScheme = SecurityParser.getSecurityScheme(securitySchemeAnnotation);
if (securityScheme.isPresent()) {
Map<String, SecurityScheme> securitySchemeMap = new HashMap<>();
if (StringUtils.isNotBlank(securityScheme.get().key)) {
securitySchemeMap.put(securityScheme.get().key, securityScheme.get().securityScheme);
if (components.getSecuritySchemes() != null && components.getSecuritySchemes().size() != 0) {
components.getSecuritySchemes().putAll(securitySchemeMap);
} else {
components.setSecuritySchemes(securitySchemeMap);
}
}
}
}
}
// class security requirements
List<SecurityRequirement> classSecurityRequirements = new ArrayList<>();
if (apiSecurityRequirements != null) {
Optional<List<SecurityRequirement>> requirementsObject = SecurityParser.getSecurityRequirements(apiSecurityRequirements.toArray(new io.swagger.v3.oas.annotations.security.SecurityRequirement[apiSecurityRequirements.size()]));
if (requirementsObject.isPresent()) {
classSecurityRequirements = requirementsObject.get();
}
}
// class tags, consider only name to add to class operations
final Set<String> classTags = new LinkedHashSet<>();
if (apiTags != null) {
AnnotationsUtils.getTags(apiTags, false).ifPresent(tags -> tags.stream().map(Tag::getName).forEach(classTags::add));
}
// parent tags
if (isSubresource) {
if (parentTags != null) {
classTags.addAll(parentTags);
}
}
// servers
final List<io.swagger.v3.oas.models.servers.Server> classServers = new ArrayList<>();
if (apiServers != null) {
AnnotationsUtils.getServers(apiServers).ifPresent(classServers::addAll);
}
// class external docs
Optional<io.swagger.v3.oas.models.ExternalDocumentation> classExternalDocumentation = AnnotationsUtils.getExternalDocumentation(apiExternalDocs);
JavaType classType = TypeFactory.defaultInstance().constructType(cls);
BeanDescription bd = Json.mapper().getSerializationConfig().introspect(classType);
final List<Parameter> globalParameters = new ArrayList<>();
// look for constructor-level annotated properties
globalParameters.addAll(ReaderUtils.collectConstructorParameters(cls, components, classConsumes, null));
// look for field-level annotated properties
globalParameters.addAll(ReaderUtils.collectFieldParameters(cls, components, classConsumes, null));
// Make sure that the class methods are sorted for deterministic order
// See https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getMethods--
final List<Method> methods = Arrays.stream(cls.getMethods()).sorted(new MethodComparator()).collect(Collectors.toList());
// iterate class methods
for (Method method : methods) {
if (isOperationHidden(method)) {
continue;
}
AnnotatedMethod annotatedMethod = bd.findMethod(method.getName(), method.getParameterTypes());
javax.ws.rs.Produces methodProduces = ReflectionUtils.getAnnotation(method, javax.ws.rs.Produces.class);
javax.ws.rs.Consumes methodConsumes = ReflectionUtils.getAnnotation(method, javax.ws.rs.Consumes.class);
if (isMethodOverridden(method, cls)) {
continue;
}
boolean methodDeprecated = ReflectionUtils.getAnnotation(method, Deprecated.class) != null;
javax.ws.rs.Path methodPath = ReflectionUtils.getAnnotation(method, javax.ws.rs.Path.class);
String operationPath = ReaderUtils.getPath(apiPath, methodPath, parentPath, isSubresource);
// extending resource config.
if (ignoreOperationPath(operationPath, parentPath) && !isSubresource) {
continue;
}
Map<String, String> regexMap = new LinkedHashMap<>();
operationPath = PathUtils.parsePath(operationPath, regexMap);
if (operationPath != null) {
if (config != null && ReaderUtils.isIgnored(operationPath, config)) {
continue;
}
final Class<?> subResource = getSubResourceWithJaxRsSubresourceLocatorSpecs(method);
String httpMethod = ReaderUtils.extractOperationMethod(method, OpenAPIExtensions.chain());
httpMethod = (httpMethod == null && isSubresource) ? parentMethod : httpMethod;
if (StringUtils.isBlank(httpMethod) && subResource == null) {
continue;
} else if (StringUtils.isBlank(httpMethod) && subResource != null) {
Type returnType = method.getGenericReturnType();
if (annotatedMethod != null && annotatedMethod.getType() != null) {
returnType = annotatedMethod.getType();
}
if (shouldIgnoreClass(returnType.getTypeName()) && !method.getGenericReturnType().equals(subResource)) {
continue;
}
}
io.swagger.v3.oas.annotations.Operation apiOperation = ReflectionUtils.getAnnotation(method, io.swagger.v3.oas.annotations.Operation.class);
JsonView jsonViewAnnotation;
JsonView jsonViewAnnotationForRequestBody;
if (apiOperation != null && apiOperation.ignoreJsonView()) {
jsonViewAnnotation = null;
jsonViewAnnotationForRequestBody = null;
} else {
jsonViewAnnotation = ReflectionUtils.getAnnotation(method, JsonView.class);
/* If one and only one exists, use the @JsonView annotation from the method parameter annotated
with @RequestBody. Otherwise fall back to the @JsonView annotation for the method itself. */
jsonViewAnnotationForRequestBody = (JsonView) Arrays.stream(ReflectionUtils.getParameterAnnotations(method)).filter(arr -> Arrays.stream(arr).anyMatch(annotation -> annotation.annotationType().equals(io.swagger.v3.oas.annotations.parameters.RequestBody.class))).flatMap(Arrays::stream).filter(annotation -> annotation.annotationType().equals(JsonView.class)).reduce((a, b) -> null).orElse(jsonViewAnnotation);
}
Operation operation = parseMethod(method, globalParameters, methodProduces, classProduces, methodConsumes, classConsumes, classSecurityRequirements, classExternalDocumentation, classTags, classServers, isSubresource, parentRequestBody, parentResponses, jsonViewAnnotation, classResponses, annotatedMethod);
if (operation != null) {
if (classDeprecated || methodDeprecated) {
operation.setDeprecated(true);
}
List<Parameter> operationParameters = new ArrayList<>();
List<Parameter> formParameters = new ArrayList<>();
Annotation[][] paramAnnotations = ReflectionUtils.getParameterAnnotations(method);
if (annotatedMethod == null) {
// annotatedMethod not null only when method with 0-2 parameters
Type[] genericParameterTypes = method.getGenericParameterTypes();
for (int i = 0; i < genericParameterTypes.length; i++) {
final Type type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls);
io.swagger.v3.oas.annotations.Parameter paramAnnotation = AnnotationsUtils.getAnnotation(io.swagger.v3.oas.annotations.Parameter.class, paramAnnotations[i]);
Type paramType = ParameterProcessor.getParameterType(paramAnnotation, true);
if (paramType == null) {
paramType = type;
} else {
if (!(paramType instanceof Class)) {
paramType = type;
}
}
ResolvedParameter resolvedParameter = getParameters(paramType, Arrays.asList(paramAnnotations[i]), operation, classConsumes, methodConsumes, jsonViewAnnotation);
operationParameters.addAll(resolvedParameter.parameters);
// collect params to use together as request Body
formParameters.addAll(resolvedParameter.formParameters);
if (resolvedParameter.requestBody != null) {
processRequestBody(resolvedParameter.requestBody, operation, methodConsumes, classConsumes, operationParameters, paramAnnotations[i], type, jsonViewAnnotationForRequestBody, null);
}
}
} else {
for (int i = 0; i < annotatedMethod.getParameterCount(); i++) {
AnnotatedParameter param = annotatedMethod.getParameter(i);
final Type type = TypeFactory.defaultInstance().constructType(param.getParameterType(), cls);
io.swagger.v3.oas.annotations.Parameter paramAnnotation = AnnotationsUtils.getAnnotation(io.swagger.v3.oas.annotations.Parameter.class, paramAnnotations[i]);
Type paramType = ParameterProcessor.getParameterType(paramAnnotation, true);
if (paramType == null) {
paramType = type;
} else {
if (!(paramType instanceof Class)) {
paramType = type;
}
}
ResolvedParameter resolvedParameter = getParameters(paramType, Arrays.asList(paramAnnotations[i]), operation, classConsumes, methodConsumes, jsonViewAnnotation);
operationParameters.addAll(resolvedParameter.parameters);
// collect params to use together as request Body
formParameters.addAll(resolvedParameter.formParameters);
if (resolvedParameter.requestBody != null) {
processRequestBody(resolvedParameter.requestBody, operation, methodConsumes, classConsumes, operationParameters, paramAnnotations[i], type, jsonViewAnnotationForRequestBody, null);
}
}
}
// if we have form parameters, need to merge them into single schema and use as request body..
if (!formParameters.isEmpty()) {
Schema mergedSchema = new ObjectSchema();
Map<String, Encoding> encoding = new LinkedHashMap<>();
for (Parameter formParam : formParameters) {
if (formParam.getExplode() != null || (formParam.getStyle() != null) && Encoding.StyleEnum.fromString(formParam.getStyle().toString()) != null) {
Encoding e = new Encoding();
if (formParam.getExplode() != null) {
e.explode(formParam.getExplode());
}
if (formParam.getStyle() != null && Encoding.StyleEnum.fromString(formParam.getStyle().toString()) != null) {
e.style(Encoding.StyleEnum.fromString(formParam.getStyle().toString()));
}
encoding.put(formParam.getName(), e);
}
mergedSchema.addProperties(formParam.getName(), formParam.getSchema());
if (formParam.getSchema() != null && StringUtils.isNotBlank(formParam.getDescription()) && StringUtils.isBlank(formParam.getSchema().getDescription())) {
formParam.getSchema().description(formParam.getDescription());
}
if (null != formParam.getRequired() && formParam.getRequired()) {
mergedSchema.addRequiredItem(formParam.getName());
}
}
Parameter merged = new Parameter().schema(mergedSchema);
processRequestBody(merged, operation, methodConsumes, classConsumes, operationParameters, new Annotation[0], null, jsonViewAnnotationForRequestBody, encoding);
}
if (!operationParameters.isEmpty()) {
for (Parameter operationParameter : operationParameters) {
operation.addParametersItem(operationParameter);
}
}
// if subresource, merge parent parameters
if (parentParameters != null) {
for (Parameter parentParameter : parentParameters) {
operation.addParametersItem(parentParameter);
}
}
if (subResource != null && !scannedResources.contains(subResource)) {
scannedResources.add(subResource);
read(subResource, operationPath, httpMethod, true, operation.getRequestBody(), operation.getResponses(), classTags, operation.getParameters(), scannedResources);
// remove the sub resource so that it can visit it later in another path
// but we have a room for optimization in the future to reuse the scanned result
// by caching the scanned resources in the reader instance to avoid actual scanning
// the the resources again
scannedResources.remove(subResource);
// don't proceed with root resource operation, as it's handled by subresource
continue;
}
final Iterator<OpenAPIExtension> chain = OpenAPIExtensions.chain();
if (chain.hasNext()) {
final OpenAPIExtension extension = chain.next();
extension.decorateOperation(operation, method, chain);
}
PathItem pathItemObject;
if (openAPI.getPaths() != null && openAPI.getPaths().get(operationPath) != null) {
pathItemObject = openAPI.getPaths().get(operationPath);
} else {
pathItemObject = new PathItem();
}
if (StringUtils.isBlank(httpMethod)) {
continue;
}
setPathItemOperation(pathItemObject, httpMethod, operation);
paths.addPathItem(operationPath, pathItemObject);
if (openAPI.getPaths() != null) {
this.paths.putAll(openAPI.getPaths());
}
openAPI.setPaths(this.paths);
}
}
}
// if no components object is defined in openApi instance passed by client, set openAPI.components to resolved components (if not empty)
if (!isEmptyComponents(components) && openAPI.getComponents() == null) {
openAPI.setComponents(components);
}
// add tags from class to definition tags
AnnotationsUtils.getTags(apiTags, true).ifPresent(tags -> openApiTags.addAll(tags));
if (!openApiTags.isEmpty()) {
Set<Tag> tagsSet = new LinkedHashSet<>();
if (openAPI.getTags() != null) {
for (Tag tag : openAPI.getTags()) {
if (tagsSet.stream().noneMatch(t -> t.getName().equals(tag.getName()))) {
tagsSet.add(tag);
}
}
}
for (Tag tag : openApiTags) {
if (tagsSet.stream().noneMatch(t -> t.getName().equals(tag.getName()))) {
tagsSet.add(tag);
}
}
openAPI.setTags(new ArrayList<>(tagsSet));
}
return openAPI;
}
use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.
the class ModelExampleTest method createModelWithExample.
@Test(description = "it should create a model with example")
public void createModelWithExample() {
ObjectSchema model = new ObjectSchema();
model.addProperties("name", new StringSchema().example("Tony"));
model.addProperties("id", new IntegerSchema().example(123));
model.example("{\"name\":\"Fred\",\"id\":123456\"}");
assertEquals(model.getExample(), "{\"name\":\"Fred\",\"id\":123456\"}");
}
use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.
the class OpenAPI3_1SerializationTest method testComponentPathItemsSerialization.
@Test
public void testComponentPathItemsSerialization() {
Schema schema = new StringSchema();
schema.addType(schema.getType());
OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("stringTest", schema).addPathItem("/pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))).addResponses("201", new ApiResponse().description("api response description")).addParameters("param", new Parameter().in("query").description("parameter description").schema(schema)).addExamples("example", new Example().summary("example summary").value("This is an example/").description("example description")).addRequestBodies("body", new RequestBody().content(new Content().addMediaType("application/json", new MediaType().schema(new ObjectSchema())))).addHeaders("test-head", new Header().description("test header description")).addSecuritySchemes("basic", new SecurityScheme().in(SecurityScheme.In.HEADER).scheme("http").description("ref security description")).addLinks("Link", new Link().operationRef("#/paths/~12.0~1repositories~1{username}/get")).addCallbacks("TestCallback", new Callback().addPathItem("{$request.query.queryUrl}", new PathItem().description("test path item").post(new Operation().operationId("testPathItem")))));
SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + " schemas:\n" + " stringTest:\n" + " type: string\n" + " responses:\n" + " \"201\":\n" + " description: api response description\n" + " parameters:\n" + " param:\n" + " in: query\n" + " description: parameter description\n" + " schema:\n" + " type: string\n" + " examples:\n" + " example:\n" + " summary: example summary\n" + " description: example description\n" + " value: This is an example/\n" + " requestBodies:\n" + " body:\n" + " content:\n" + " application/json:\n" + " schema: {}\n" + " headers:\n" + " test-head:\n" + " description: test header description\n" + " securitySchemes:\n" + " basic:\n" + " description: ref security description\n" + " in: header\n" + " scheme: http\n" + " links:\n" + " Link:\n" + " operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + " callbacks:\n" + " TestCallback:\n" + " '{$request.query.queryUrl}':\n" + " description: test path item\n" + " post:\n" + " operationId: testPathItem\n" + " pathItems:\n" + " /pathTest:\n" + " description: test path item\n" + " get:\n" + " operationId: testPathItem\n" + " responses:\n" + " \"200\":\n" + " description: response description");
SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + " \"openapi\" : \"3.1.0\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"stringTest\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " },\n" + " \"responses\" : {\n" + " \"201\" : {\n" + " \"description\" : \"api response description\"\n" + " }\n" + " },\n" + " \"parameters\" : {\n" + " \"param\" : {\n" + " \"in\" : \"query\",\n" + " \"description\" : \"parameter description\",\n" + " \"schema\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"examples\" : {\n" + " \"example\" : {\n" + " \"summary\" : \"example summary\",\n" + " \"description\" : \"example description\",\n" + " \"value\" : \"This is an example/\"\n" + " }\n" + " },\n" + " \"requestBodies\" : {\n" + " \"body\" : {\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : { }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"headers\" : {\n" + " \"test-head\" : {\n" + " \"description\" : \"test header description\"\n" + " }\n" + " },\n" + " \"securitySchemes\" : {\n" + " \"basic\" : {\n" + " \"description\" : \"ref security description\",\n" + " \"in\" : \"header\",\n" + " \"scheme\" : \"http\"\n" + " }\n" + " },\n" + " \"links\" : {\n" + " \"Link\" : {\n" + " \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + " }\n" + " },\n" + " \"callbacks\" : {\n" + " \"TestCallback\" : {\n" + " \"{$request.query.queryUrl}\" : {\n" + " \"description\" : \"test path item\",\n" + " \"post\" : {\n" + " \"operationId\" : \"testPathItem\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"pathItems\" : {\n" + " \"/pathTest\" : {\n" + " \"description\" : \"test path item\",\n" + " \"get\" : {\n" + " \"operationId\" : \"testPathItem\",\n" + " \"responses\" : {\n" + " \"200\" : {\n" + " \"description\" : \"response description\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}");
openAPI.openapi("3.0.3");
SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + " schemas:\n" + " stringTest:\n" + " type: string\n" + " responses:\n" + " \"201\":\n" + " description: api response description\n" + " parameters:\n" + " param:\n" + " in: query\n" + " description: parameter description\n" + " schema:\n" + " type: string\n" + " examples:\n" + " example:\n" + " summary: example summary\n" + " description: example description\n" + " value: This is an example/\n" + " requestBodies:\n" + " body:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " type: object\n" + " headers:\n" + " test-head:\n" + " description: test header description\n" + " securitySchemes:\n" + " basic:\n" + " description: ref security description\n" + " in: header\n" + " scheme: http\n" + " links:\n" + " Link:\n" + " operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + " callbacks:\n" + " TestCallback:\n" + " '{$request.query.queryUrl}':\n" + " description: test path item\n" + " post:\n" + " operationId: testPathItem");
SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + " \"openapi\" : \"3.0.3\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"stringTest\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " },\n" + " \"responses\" : {\n" + " \"201\" : {\n" + " \"description\" : \"api response description\"\n" + " }\n" + " },\n" + " \"parameters\" : {\n" + " \"param\" : {\n" + " \"in\" : \"query\",\n" + " \"description\" : \"parameter description\",\n" + " \"schema\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"examples\" : {\n" + " \"example\" : {\n" + " \"summary\" : \"example summary\",\n" + " \"description\" : \"example description\",\n" + " \"value\" : \"This is an example/\"\n" + " }\n" + " },\n" + " \"requestBodies\" : {\n" + " \"body\" : {\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : {\n" + " \"type\" : \"object\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"headers\" : {\n" + " \"test-head\" : {\n" + " \"description\" : \"test header description\"\n" + " }\n" + " },\n" + " \"securitySchemes\" : {\n" + " \"basic\" : {\n" + " \"description\" : \"ref security description\",\n" + " \"in\" : \"header\",\n" + " \"scheme\" : \"http\"\n" + " }\n" + " },\n" + " \"links\" : {\n" + " \"Link\" : {\n" + " \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + " }\n" + " },\n" + " \"callbacks\" : {\n" + " \"TestCallback\" : {\n" + " \"{$request.query.queryUrl}\" : {\n" + " \"description\" : \"test path item\",\n" + " \"post\" : {\n" + " \"operationId\" : \"testPathItem\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}");
}
use of io.swagger.v3.oas.models.media.ObjectSchema in project swagger-core by swagger-api.
the class PropertySerializationTest method serializeObjectPropertyWithRequiredProperties.
@Test(description = "it should serialize an object property with required set")
public void serializeObjectPropertyWithRequiredProperties() throws IOException {
final Schema p = new ObjectSchema().addProperties("stringProperty", new StringSchema());
p.required(Arrays.asList("stringProperty"));
final String json = "{\"required\":[\"stringProperty\"],\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}}}";
assertEquals(m.writeValueAsString(p), json);
}
Aggregations