Skip to content

Commit

Permalink
Omit extensions in forms by default for now
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcizek committed Jul 20, 2023
1 parent 8424bf9 commit 468b91e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 704 deletions.
13 changes: 9 additions & 4 deletions form-api/src/main/java/ai/stapi/formapi/FormEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -30,17 +31,21 @@ public FormEndpoint(
this.operationDefinitionProvider = operationDefinitionProvider;
}

@GetMapping("/form/{operationId}/")
@GetMapping({
"/form/{operationId}",
"/form/{operationId}/{resourceId}",
})
@ResponseBody
public Map<String, Object> form(
@PathVariable String operationId,
@PathVariable String resourceId,
@PathVariable String startModificationNodeIdAndType
@PathVariable(required = false) String resourceId,
@RequestParam(required = false) String startModificationNodeIdAndType,
@RequestParam(required = false, defaultValue = "true") Boolean omitExtension
) {
var operation = this.operationDefinitionProvider.provide(operationId);

return Map.of(
"formSchema", this.jsonSchemaMapper.map(operation),
"formSchema", this.jsonSchemaMapper.map(operation, omitExtension),
"uiSchema", this.uiSchemaLoader.load(operation),
"formData", this.formDataLoader.load(operation, resourceId, startModificationNodeIdAndType)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,22 @@ public JsonSchemaMapper(
}

public Map<String, Object> map(OperationDefinitionDTO operationDefinitionDTO) {
var fakedStructureType = this.operationDefinitionStructureTypeMapper.map(operationDefinitionDTO);
return this.map(operationDefinitionDTO, true);
}

public Map<String, Object> map(OperationDefinitionDTO operationDefinitionDTO,
Boolean omitExtension) {
var fakedStructureType = this.operationDefinitionStructureTypeMapper.map(
operationDefinitionDTO);
var formMapperContext = new FormMapperContext();
var schema = this.getObjectSchema(fakedStructureType, formMapperContext);
var schema = this.getObjectSchema(fakedStructureType, formMapperContext, omitExtension);
return this.printSchema(schema, formMapperContext);
}

private ObjectSchema getObjectSchema(
ComplexStructureType complexStructureType,
FormMapperContext formMapperContext
FormMapperContext formMapperContext,
Boolean omitExtension
) {
var builder = new ObjectSchema.Builder();
builder.title(complexStructureType.getDefinitionType());
Expand All @@ -102,16 +109,19 @@ private ObjectSchema getObjectSchema(
complexStructureType.getAllFields()
.values()
.stream()
.filter(field -> !omitExtension || (!field.getName().equals("extension") &&
!field.getName().equals("modifierExtension")))
.sorted(Comparator.comparing(FieldDefinition::getName))
.forEach(field -> this.mapField(field, builder, formMapperContext));
.forEach(field -> this.mapField(field, builder, formMapperContext, omitExtension));

return builder.build();
}

private void mapField(
FieldDefinition field,
ObjectSchema.Builder builder,
FormMapperContext formMapperContext
FormMapperContext formMapperContext,
Boolean omitExtension
) {
var parameterName = field.getName();
if (field.getMin() > 0) {
Expand All @@ -121,30 +131,35 @@ private void mapField(
return;
}
if (field.getFloatMax() > 1) {
this.mapArrayField(field, builder, formMapperContext);
this.mapArrayField(field, builder, formMapperContext, omitExtension);
return;
}
var schema = this.getSchema(field, formMapperContext);
var schema = this.getSchema(field, formMapperContext, omitExtension);
builder.addPropertySchema(parameterName, schema);
}

private Schema getSchema(FieldDefinition field, FormMapperContext formMapperContext) {
private Schema getSchema(
FieldDefinition field,
FormMapperContext formMapperContext,
Boolean omitExtension
) {
if (field.getTypes().size() > 1) {
var schemaBuilder = new CombinedSchema.Builder().criterion(CombinedSchema.ONE_CRITERION);
field.getTypes().stream()
.map(type -> this.getMemberSchema(type, field, formMapperContext))
.map(type -> this.getMemberSchema(type, field, formMapperContext, omitExtension))
.forEach(schemaBuilder::subschema);

return schemaBuilder.build();
}
var type = field.getTypes().get(0);
return this.getMemberSchema(type, field, formMapperContext);
return this.getMemberSchema(type, field, formMapperContext, omitExtension);
}

private Schema getMemberSchema(
FieldType type,
FieldDefinition fieldDefinition,
FormMapperContext formMapperContext
FormMapperContext formMapperContext,
Boolean omitExtension
) {
var typeName = type.getType();
if (type.isPrimitiveType()) {
Expand All @@ -154,8 +169,9 @@ private Schema getMemberSchema(
} else {
if (!formMapperContext.hasType(typeName)) {
formMapperContext.addType(typeName);
var structureType = (ComplexStructureType) this.structureSchemaFinder.getStructureType(typeName);
var objectSchema = this.getObjectSchema(structureType, formMapperContext);
var structureType = (ComplexStructureType) this.structureSchemaFinder.getStructureType(
typeName);
var objectSchema = this.getObjectSchema(structureType, formMapperContext, omitExtension);
formMapperContext.putSchema(typeName, objectSchema);
}
return new ReferenceSchema.Builder()
Expand Down Expand Up @@ -191,9 +207,10 @@ private Schema getPrimitiveSchema(String type, FieldDefinition fieldDefinition)
private void mapArrayField(
FieldDefinition field,
ObjectSchema.Builder builder,
FormMapperContext formMapperContext
FormMapperContext formMapperContext,
Boolean omitExtension
) {
var itemSchema = this.getSchema(field, formMapperContext);
var itemSchema = this.getSchema(field, formMapperContext, omitExtension);
builder.addPropertySchema(
field.getName(),
new ArraySchema.Builder().allItemSchema(itemSchema).build()
Expand Down
Loading

0 comments on commit 468b91e

Please sign in to comment.