Uploaded image for project: 'Spring Data MongoDB'
  1. Spring Data MongoDB
  2. DATAMONGO-1849

Generate $jsonSchema out of domain type meta information.

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: None
    • Fix Version/s: 2.2 M3 (Moore)
    • Component/s: Core
    • Labels:

      Description

      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.

      MongoJsonSchemaMapper
      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
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                cstrobl Christoph Strobl
                Reporter:
                cstrobl Christoph Strobl
                Last updater:
                Mark Paluch
              • Votes:
                2 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: