Java Code Examples for io.swagger.v3.oas.models.media.Schema#setDescription()

The following examples show how to use io.swagger.v3.oas.models.media.Schema#setDescription() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericParameterBuilder.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate request body schema schema.
 *
 * @param components the components
 * @param parameterInfo the parameter info
 * @param requestBodyInfo the request body info
 * @param schemaN the schema n
 * @param paramName the param name
 * @return the schema
 */
private Schema calculateRequestBodySchema(Components components, ParameterInfo parameterInfo, RequestBodyInfo requestBodyInfo, Schema schemaN, String paramName) {
	if (schemaN != null && StringUtils.isEmpty(schemaN.getDescription()) && parameterInfo.getParameterModel() != null) {
		String description = parameterInfo.getParameterModel().getDescription();
		if (schemaN.get$ref() != null && schemaN.get$ref().contains(AnnotationsUtils.COMPONENTS_REF)) {
			String key = schemaN.get$ref().substring(21);
			Schema existingSchema = components.getSchemas().get(key);
			existingSchema.setDescription(description);
		}
		else
			schemaN.setDescription(description);
	}

	if (requestBodyInfo.getMergedSchema() != null) {
		requestBodyInfo.getMergedSchema().addProperties(paramName, schemaN);
		schemaN = requestBodyInfo.getMergedSchema();
	}
	else if (schemaN instanceof FileSchema || schemaN instanceof ArraySchema && ((ArraySchema) schemaN).getItems() instanceof FileSchema) {
		schemaN = new ObjectSchema().addProperties(paramName, schemaN);
		requestBodyInfo.setMergedSchema(schemaN);
	}
	else
		requestBodyInfo.addProperties(paramName, schemaN);
	return schemaN;
}
 
Example 2
Source File: SwaggerAnnotationUtils.java    From servicecomb-toolkit with Apache License 2.0 6 votes vote down vote up
public static Schema getSchemaFromAnnotation(io.swagger.v3.oas.annotations.media.Schema schema) {
  if (schema == null) {
    return null;
  }
  Schema schemaObj = new Schema();
  schemaObj.setName(schema.name());
  schemaObj.setDescription(schema.description());
  schemaObj.setType(schema.type());
  schemaObj.setTitle(schema.title());
  schemaObj.setNullable(schema.nullable());
  schemaObj.setDefault(schema.defaultValue());
  schemaObj.setFormat(schema.format());
  schemaObj.setDeprecated(schema.deprecated());
  Map<String, Object> extensionsFromAnnotation = getExtensionsFromAnnotation(schema.extensions());
  schemaObj.extensions(extensionsFromAnnotation);
  return schemaObj;
}
 
Example 3
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArbitraryObjectModelWithArrayInlineWithTitle() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema items = new ObjectSchema();
    items.setTitle("InnerUserTitle");
    items.setDefault("default");
    items.setReadOnly(false);
    items.setDescription("description");
    items.setName("name");
    items.addProperties("arbitrary", new ObjectSchema());

    openAPI.getComponents().addSchemas("User", new ArraySchema().items(items).addRequiredItem("name"));

    new InlineModelResolver().flatten(openAPI);

    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);
    ArraySchema am = (ArraySchema) model;
    Schema inner = am.getItems();
    assertTrue(inner.get$ref() != null);

    Schema userInner = openAPI.getComponents().getSchemas().get("InnerUserTitle");
    assertNotNull(userInner);
    Schema inlineProp = (Schema) userInner.getProperties().get("arbitrary");
    assertTrue(inlineProp instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) inlineProp;
    assertNull(op.getProperties());
}
 
Example 4
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArbitraryObjectModelWithArrayInlineWithoutTitle() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema items = new ObjectSchema();
    items.setDefault("default");
    items.setReadOnly(false);
    items.setDescription("description");
    items.setName("name");
    items.addProperties("arbitrary", new ObjectSchema());

    openAPI.getComponents().addSchemas("User", new ArraySchema().items(items).addRequiredItem("name"));

    new InlineModelResolver().flatten(openAPI);

    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);
    ArraySchema am = (ArraySchema) model;
    Schema inner = am.getItems();
    assertTrue(inner.get$ref() != null);

    Schema userInner = openAPI.getComponents().getSchemas().get("User_inner");
    assertNotNull(userInner);
    Schema inlineProp = (Schema)userInner.getProperties().get("arbitrary");
    assertTrue(inlineProp instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) inlineProp;
    assertNull(op.getProperties());
}
 
Example 5
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArbitraryObjectModelInline() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema userSchema = new Schema();
    userSchema.setName("user");
    userSchema.setDescription("a common user");
    userSchema.addProperties("name", new StringSchema());

    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.setTitle("title");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");

    userSchema.addProperties("arbitrary", objectSchema);
    List required = new ArrayList();
    required.add("arbitrary");
    userSchema.setRequired(required);


    openAPI.getComponents().addSchemas("User", userSchema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");
    assertNotNull(user);
    Schema inlineProp = (Schema) user.getProperties().get("arbitrary");
    assertTrue(inlineProp instanceof ObjectSchema);
    assertNull(inlineProp.getProperties());
}
 
Example 6
Source File: StaticHtmlGenerator.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public void preprocessOpenAPI(OpenAPI openAPI) {
    Info info = openAPI.getInfo();
    info.setDescription(toHtml(info.getDescription()));
    info.setTitle(toHtml(info.getTitle()));
    Map<String, Schema> models = ModelUtils.getSchemas(openAPI);
    for (Schema model : models.values()) {
        model.setDescription(toHtml(model.getDescription()));
        model.setTitle(toHtml(model.getTitle()));
    }
}
 
Example 7
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineArrayModelWithoutTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema objectSchema = new ObjectSchema();
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    ArraySchema arraySchema =  new ArraySchema();
    List<String> required = new LinkedList<>();
    required.add("name");
    arraySchema.setRequired(required);
    arraySchema.setItems(objectSchema);

    openAPI.getComponents().addSchemas("User", arraySchema);


    new InlineModelResolver().flatten(openAPI);

    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);

    Schema user = openAPI.getComponents().getSchemas().get("User_inner");
    assertNotNull(user);
    assertEquals("description", user.getDescription());
}
 
Example 8
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 5 votes vote down vote up
private Schema parseTypeToSchema(Type javaType, String description) {
    try {
        Schema schema = parseResolvedTypeToSchema(javaType.resolve());
        if (GeneratorUtils.isNotBlank(description)) {
            schema.setDescription(description);
        }
        return schema;
    } catch (Exception e) {
        getLogger().info(String.format(
                "Can't resolve type '%s' for creating custom OpenAPI Schema. Using the default ObjectSchema instead.",
                javaType.asString()), e);
    }
    return new ObjectSchema();
}
 
Example 9
Source File: SwaggerConverter.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
private Header convertHeader(Property property) {
    Schema schema = convert(property);
    schema.setDescription(null);

    Header header = new Header();
    header.setDescription(property.getDescription());
    header.setSchema(schema);

    return header;
}
 
Example 10
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineModelTestWithoutTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());


    Schema objectSchema = new ObjectSchema();
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    Schema schema =  new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);


    openAPI.getComponents().addSchemas("User", schema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema address = (Schema)user.getProperties().get("address");
    assertTrue((address.get$ref()!= null));
    Schema userAddress = openAPI.getComponents().getSchemas().get("User_address");
    assertNotNull(userAddress);
    assertNotNull(userAddress.getProperties().get("city"));
    assertNotNull(userAddress.getProperties().get("street"));
}
 
Example 11
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineModelTestWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());


    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("UserAddressTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    Schema schema =  new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);


    openAPI.getComponents().addSchemas("User", schema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema address = (Schema)user.getProperties().get("address");
    assertTrue( address.get$ref() != null);

    Schema userAddressTitle = openAPI.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(userAddressTitle);
    assertNotNull(userAddressTitle.getProperties().get("city"));
    assertNotNull(userAddressTitle.getProperties().get("street"));
}
 
Example 12
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineArrayModelWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("InnerUserTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    ArraySchema arraySchema =  new ArraySchema();
    List<String> required = new LinkedList<>();
    required.add("name");
    arraySchema.setRequired(required);
    arraySchema.setItems(objectSchema);


    openAPI.getComponents().addSchemas("User", arraySchema);


    new InlineModelResolver().flatten(openAPI);

    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);

    Schema user = openAPI.getComponents().getSchemas().get("InnerUserTitle");
    assertNotNull(user);
    assertEquals("description", user.getDescription());
}
 
Example 13
Source File: InlineModelResolver.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public Schema createModelFromProperty(Schema schema, String path) {
    String description = schema.getDescription();
    String example = null;
    List<String> requiredList = schema.getRequired();


    Object obj = schema.getExample();
    if (obj != null) {
        example = obj.toString();
    }
    String name = schema.getName();
    XML xml = schema.getXml();
    Map<String, Schema> properties = schema.getProperties();


    if (schema instanceof ComposedSchema && this.flattenComposedSchemas){
        ComposedSchema composedModel = (ComposedSchema) schema;

        composedModel.setDescription(description);
        composedModel.setExample(example);
        composedModel.setName(name);
        composedModel.setXml(xml);
        composedModel.setType(schema.getType());
        composedModel.setRequired(requiredList);

        return composedModel;


    } else {
        Schema model = new Schema();//TODO Verify this!
        model.setDescription(description);
        model.setExample(example);
        model.setName(name);
        model.setXml(xml);
        model.setType(schema.getType());
        model.setRequired(requiredList);

        if (properties != null) {
            flattenProperties(properties, path);
            model.setProperties(properties);
        }

        return model;
    }
}
 
Example 14
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void resolveInlineModel2DifferentInnerModelsWIthSameTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("UserAddressTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    Schema schema =  new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);


    openAPI.getComponents().addSchemas("User", schema);

    Schema addressSchema = new ObjectSchema();
    addressSchema.setTitle("UserAddressTitle");
    addressSchema.setDefault("default");
    addressSchema.setReadOnly(false);
    addressSchema.setDescription("description");
    addressSchema.setName("name");
    addressSchema.addProperties("street", new StringSchema());
    addressSchema.addProperties("city", new StringSchema());
    addressSchema.addProperties("apartment", new StringSchema());


    Schema anotherSchema =  new Schema();
    anotherSchema.setName("AnotherUser");
    anotherSchema.setDescription("a common user");
    List<String> requiredFields = new ArrayList<>();
    requiredFields.add("address");
    anotherSchema.setRequired(requiredFields);
    anotherSchema.addProperties("name", new StringSchema());
    anotherSchema.addProperties("lastName", new StringSchema());
    anotherSchema.addProperties("address", addressSchema);


    openAPI.getComponents().addSchemas("AnotherUser", anotherSchema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema userAddress = (Schema) user.getProperties().get("address");
    assertTrue( userAddress.get$ref()!= null);

    Schema address = openAPI.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(address);
    assertNotNull(address.getProperties().get("city"));
    assertNotNull(address.getProperties().get("street"));
    Schema duplicateAddress = openAPI.getComponents().getSchemas().get("UserAddressTitle_1");
    assertNotNull(duplicateAddress);
    assertNotNull(duplicateAddress.getProperties().get("city"));
    assertNotNull(duplicateAddress.getProperties().get("street"));
    assertNotNull(duplicateAddress.getProperties().get("apartment"));
}
 
Example 15
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void resolveInlineModel2EqualInnerModels() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());


    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("UserAddressTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    Schema schema =  new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);


    openAPI.getComponents().addSchemas("User", schema);

    Schema addressSchema = new ObjectSchema();
    addressSchema.setTitle("UserAddressTitle");
    addressSchema.setDefault("default");
    addressSchema.setReadOnly(false);
    addressSchema.setDescription("description");
    addressSchema.setName("name");
    addressSchema.addProperties("street", new StringSchema());
    addressSchema.addProperties("city", new StringSchema());

    Schema anotherSchema =  new Schema();
    anotherSchema.setName("user");
    anotherSchema.setDescription("a common user");
    List<String> requiredFields = new ArrayList<>();
    requiredFields.add("address");
    anotherSchema.setRequired(requiredFields);
    anotherSchema.addProperties("name", new StringSchema());
    anotherSchema.addProperties("lastName", new StringSchema());
    anotherSchema.addProperties("address", addressSchema);

    openAPI.getComponents().addSchemas("AnotherUser", anotherSchema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema addressSchema1 = (Schema) user.getProperties().get("address");
    assertTrue(addressSchema1.get$ref() != null);

    Schema address = openAPI.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(address);
    assertNotNull(address.getProperties().get("city"));
    assertNotNull(address.getProperties().get("street"));
    Schema duplicateAddress = openAPI.getComponents().getSchemas().get("UserAddressTitle_0");
    assertNull(duplicateAddress);
}
 
Example 16
Source File: OASMergeUtil.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
public static Schema mergeSchema(Schema thisSchema, Schema thatSchema) {
  if (thatSchema == null) {
    return thisSchema;
  }
  // Overwriting `implementation` is explicitly disallowed
  // Overwriting `not` is explicitly disallowed
  // Overwriting `oneOf` is explicitly disallowed
  // Overwriting `anyOf` is explicitly disallowed
  // Overwriting `allOf` is explicitly disallowed
  // Overwriting `name` is explicitly disallowed
  if (thatSchema.getTitle() != null) {
    thisSchema.setTitle(thatSchema.getTitle());
  }
  // Overwriting `multipleOf` is explicitly disallowed
  if (thatSchema.getMaximum() != null) {
    thisSchema.setMaximum(thatSchema.getMaximum());
  }
  if (thatSchema.getExclusiveMaximum() != null) {
    thisSchema.setExclusiveMaximum(thatSchema.getExclusiveMaximum());
  }

  if (thatSchema.getMinimum() != null) {
    thisSchema.setMinimum(thatSchema.getMinimum());
  }
  if (thatSchema.getExclusiveMinimum() != null) {
    thisSchema.setExclusiveMinimum(thatSchema.getExclusiveMinimum());
  }
  if (thatSchema.getMaxLength() != null) {
    thisSchema.setMaxLength(thatSchema.getMaxLength());
  }
  if (thatSchema.getMinLength() != null) {
    thisSchema.setMinLength(thatSchema.getMinLength());
  }
  if (thatSchema.getPattern() != null) {
    thisSchema.setPattern(thatSchema.getPattern());
  }
  if (thatSchema.getMaxProperties() != null) {
    thisSchema.setMaxProperties(thatSchema.getMaxProperties());
  }
  if (thatSchema.getMinProperties() != null) {
    thisSchema.setMinProperties(thatSchema.getMinProperties());
  }
  // RequiredProperties
  if (thatSchema.getRequired() != null) {
    thisSchema.setRequired(thatSchema.getRequired());
  }
  // Overwriting `name` is explicitly disallowed
  if (thatSchema.getDescription() != null) {
    thisSchema.setDescription(thatSchema.getDescription());
  }
  if (thatSchema.getFormat() != null) {
    thisSchema.setFormat(thatSchema.getFormat());
  }
  // Overwriting `ref` is explicitly disallowed
  if (thatSchema.getNullable() != null) {
    thisSchema.setNullable(thatSchema.getNullable());
  }
  // Overwriting `AccessMode` is explicitly disallowed
  if (thatSchema.getExample() != null) {
    thisSchema.setExample(thatSchema.getExample());
  }
  if (thatSchema.getExternalDocs() != null) {
    thisSchema.setExternalDocs(thatSchema.getExternalDocs());
  }
  if (thatSchema.getDeprecated() != null) {
    thisSchema.setDeprecated(thatSchema.getDeprecated());
  }
  if (thatSchema.getType() != null) {
    thisSchema.setType(thatSchema.getType());
  }
  if (thatSchema.getEnum() != null) {
    thisSchema.setEnum(thatSchema.getEnum());
  }
  if (thatSchema.getDefault() != null) {
    thisSchema.setDefault(thatSchema.getDefault());
  }
  // Overwriting `discriminator` is explicitly disallowed
  // Overwriting `hidden` is explicitly disallowed
  // Overwriting `subTypes` is explicitly disallowed
  if (thatSchema.getExtensions() != null) {
    thisSchema.setExtensions(thatSchema.getExtensions());
  }
  return thisSchema;
}
 
Example 17
Source File: TestSchemaFieldInterceptor.java    From spring-openapi with MIT License 4 votes vote down vote up
@Override
public void intercept(Class<?> clazz, Field field, Schema<?> transformedFieldSchema) {
	transformedFieldSchema.setDescription("Schema field description");
}
 
Example 18
Source File: TestSchemaFieldInterceptor.java    From spring-openapi with MIT License 4 votes vote down vote up
@Override
public void intercept(Class<?> clazz, Field field, Schema<?> transformedFieldSchema) {
	transformedFieldSchema.setDescription(emptyIfNull(transformedFieldSchema.getDescription()) + ". Test schemaField interceptor");
}
 
Example 19
Source File: TestSchemaInterceptor.java    From spring-openapi with MIT License 4 votes vote down vote up
@Override
public void intercept(Class<?> clazz, Schema<?> transformedSchema) {
	transformedSchema.setDescription(emptyIfNull(transformedSchema.getDescription()) + ". Test schema interceptors");
}