Skip to content

Commit 860bb61

Browse files
committed
Make generation of @type attribute in JSON payload configurable
1 parent d1395f5 commit 860bb61

File tree

4 files changed

+101
-45
lines changed

4 files changed

+101
-45
lines changed

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/json/AspectModelJsonPayloadGenerator.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ public Stream<JsonPayloadArtifact> generate() {
107107
return Stream.of( new PayloadGenerator( objectMapper ).apply( aspect(), config ) );
108108
}
109109

110-
private static class PayloadGenerator
111-
implements ArtifactGenerator<String, JsonNode, Aspect, JsonPayloadGenerationConfig, JsonPayloadArtifact> {
110+
private class PayloadGenerator implements ArtifactGenerator<String, JsonNode, Aspect, JsonPayloadGenerationConfig, JsonPayloadArtifact> {
112111
/**
113112
* Constant for the either left property.
114113
* For example JSON payloads the left type will be used.
@@ -226,7 +225,9 @@ private Map<String, Object> transformAbstractEntityProperty( final BasicProperty
226225
final ComplexType extendingComplexType = abstractEntity.getExtendingElements().get( 0 );
227226
final Map<String, Object> generatedProperties = transformProperties( extendingComplexType.getAllProperties(),
228227
useModelExampleValue );
229-
generatedProperties.put( "@type", extendingComplexType.getName() );
228+
if ( config.addTypeAttributeForEntityInheritance() ) {
229+
generatedProperties.put( "@type", extendingComplexType.getName() );
230+
}
230231
return toMap( property.getName(), generatedProperties );
231232
}
232233
return ImmutableMap.of();
@@ -237,7 +238,7 @@ private Map<String, Object> transformEntityProperty( final BasicProperty propert
237238
if ( dataType.isPresent() ) {
238239
final Entity entity = dataType.get();
239240
final Map<String, Object> generatedProperties = transformProperties( entity.getAllProperties(), useModelExmplevalue );
240-
if ( entity.getExtends().isPresent() ) {
241+
if ( entity.getExtends().isPresent() && config.addTypeAttributeForEntityInheritance() ) {
241242
generatedProperties.put( "@type", entity.getName() );
242243
}
243244
return toMap( property.getName(), generatedProperties );
@@ -331,7 +332,9 @@ private Object generateCollectionValue( final Type dataType, final int minCount
331332
final AbstractEntity abstractEntity = dataType.as( AbstractEntity.class );
332333
final ComplexType extendingComplexType = abstractEntity.getExtendingElements().get( 0 );
333334
final Map<String, Object> propertyValueMap = transformProperties( extendingComplexType.getAllProperties(), minCount < 2 );
334-
propertyValueMap.put( "@type", extendingComplexType.getName() );
335+
if ( config.addTypeAttributeForEntityInheritance() ) {
336+
propertyValueMap.put( "@type", extendingComplexType.getName() );
337+
}
335338
return propertyValueMap;
336339
}
337340
if ( dataType.is( Entity.class ) ) {

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/json/JsonPayloadGenerationConfig.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
import io.soabase.recordbuilder.core.RecordBuilder;
2222

2323
/**
24-
* A {@link GenerationConfig} for JSON sample payload generation
24+
* A {@link GenerationConfig} for JSON sample payload generation.
25+
*
26+
* @param randomStrategy the Random instance to use for random value generation
27+
* @param addTypeAttributeForEntityInheritance if set to true, adds "@type" attribute in payloads for inherited entities
2528
*/
2629
@RecordBuilder
2730
public record JsonPayloadGenerationConfig(
28-
Random randomStrategy
31+
Random randomStrategy,
32+
boolean addTypeAttributeForEntityInheritance
2933
) implements JsonGenerationConfig {
3034
public JsonPayloadGenerationConfig {
3135
if ( randomStrategy == null ) {

0 commit comments

Comments
 (0)