Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate $jsonSchema out of domain type meta information. [DATAMONGO-1849] #2753

Closed
spring-projects-issues opened this issue Jan 16, 2018 · 1 comment
Assignees
Labels
in: core Issues in core support type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

spring-projects-issues commented Jan 16, 2018

Christoph Strobl opened DATAMONGO-1849 and commented

Use the information from MongoPersistentEntity and MongoPersistentProperty to generate JSON Schema for a given type using annotations like @Nullable to distinguish between required and optional fields.

MongoJsonSchema fromType(Class<?> domainType) {
		
	MongoPersistentEntity<?> entity  = mappingContext.getPersistentEntity(domainType);
	
	MongoJsonSchemaBuilder builder = MongoJsonSchema.builder();
	
	entity.doWithProperties((SimplePropertyHandler) persistentProperty -> {

		// if property is constructor arg or not @Nullable make it required
		if(entity.isConstructorArgument(persistentProperty) || persistentProperty.findAnnotation(Nullable.class) == null) {
			builder.required(persistentProperty.getName());
		}
		
		
		// append a collection type property
		if(persistentProperty.isArray() || persistentProperty.isCollectionLike()) {
			ArrayJsonSchemaProperty arrayProperty = JsonSchemaProperty.array(persistentProperty.getName()); //
			if(ClassUtils.isAssignable(Set.class, persistentProperty.getActualType())) {
				arrayProperty = arrayProperty.uniqueItems(true);
			}
			builder.property(arrayProperty);
		}
		
		if(persistentProperty.isEntity()) {
			
			// .... and so on

Reference URL: https://stackoverflow.com/questions/50169457/define-mongo-schema-validation-using-spring

Issue Links:

  • DATACMNS-1513 Expose nullability requirements for PersistentProperty and PreferredConstructor arguments

  • DATAMONGO-1322 Add support for validator when creating collection

  • DATAMONGO-1835 Add support for $jsonSchema to Criteria API

Referenced from: pull request #733

2 votes, 2 watchers

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Jan 16, 2018

Christoph Strobl commented

Rules for schema generation:

General Rules

Required Properties
  • constructor arguments unless annotated with @Nullable, @Null
  • all @NonNull @NotNull annotated fields
  • primitive types
Ignored Properties
  • @Transient fields
Type Mapping
  • Object.class -> UntypedJsonSchemaObject
  • Simple types -> Type.of()
  • Entity types -> object
  • List / array -> array
  • enum -> string

Type Specific Rules

Enum Types
  • restrict values via enum : [] to the ones available for the enum.
String Type
  • @Size(min=...) -> minLength
  • @Size(max=...) -> maxLength
  • @Pattern(regexp=...) -> pattern
Numeric Types
  • @Max, @DecimalMax -> maximum
  • @Min, @DecimalMin -> minimum
  • @Digits -> NOT SUPPORTED
Arrays / Collections
  • @Size(min=...) -> minItems
  • @Size(max=...) -> maxItems
  • Set.class -> uniqueItems : true
Entity Type
  • non transient properties -> properties + nested mapping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants