Commit 1c3e41b1 authored by Martti Käärik's avatar Martti Käärik
Browse files

OpenAPI: Add discriminator support and inheritance handling #77

- Implemented support for OpenAPI discriminator, mapping it to TDL inheritance.
- Added detection for allOf-based inheritance during translation.
parent 084301ab
Loading
Loading
Loading
Loading
+46 −4
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.PathItem.HttpMethod;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.media.Discriminator;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
@@ -501,8 +502,12 @@ public class OpenAPI2TDLTranslatorNext extends AbstractTranslator {
		List<Schema> xOf = schema.getAllOf();
		if (xOf == null)
			xOf = schema.getAnyOf();
		// If there is a discriminator, then oneOf represents sub-types in an inheritance hierarchy.
		// In this case, we don't want to "leak" all sub-type properties into the base type.
		if (schema.getDiscriminator() == null) {
			if (xOf == null)
				xOf = schema.getOneOf();
		}
		if (xOf != null) {
			for (Schema ofSchema : xOf)
				this.getAllProperties(ofSchema, properties);
@@ -518,8 +523,12 @@ public class OpenAPI2TDLTranslatorNext extends AbstractTranslator {
		List<Schema> xOf = schema.getAllOf();
		if (xOf == null)
			xOf = schema.getAnyOf();
		// If there is a discriminator, then oneOf represents sub-types in an inheritance hierarchy.
		// In this case, we don't want to "leak" all sub-type properties into the base type.
		if (schema.getDiscriminator() == null) {
			if (xOf == null)
				xOf = schema.getOneOf();
		}
		if (xOf != null) {
			for (Schema ofSchema : xOf)
				this.getAllRequired(ofSchema, required);
@@ -529,6 +538,39 @@ public class OpenAPI2TDLTranslatorNext extends AbstractTranslator {
	}

	private DataType translate(Schema<?> schema, String prefix, DataType superType) {
		Discriminator discriminator = schema.getDiscriminator();
		if (discriminator != null) {
			DataType baseType = translateObject(schema, schema.getName());
			addSuperType(baseType, superType);

			List<Schema> subTypes = schema.getOneOf();
			if (subTypes != null) {
				for (Schema subSchemaRef : subTypes) {
					Schema<?> subSchema = ModelUtils.getReferencedSchema(model, subSchemaRef);
					subSchema.setName(ModelUtils.getSimpleRef(subSchemaRef.get$ref()));
					translate(subSchema, "", baseType);
				}
			}
			return baseType;
		}

		List<Schema> allOf = schema.getAllOf();
		if (allOf != null) {
			Schema<?> refSchema = null;
			int refCount = 0;
			for (Schema<?> s : allOf) {
				if (s.get$ref() != null) {
					refCount++;
					refSchema = s;
				}
			}
			if (refCount == 1) {
				Schema<?> referencedSchema = ModelUtils.getReferencedSchema(model, refSchema);
				referencedSchema.setName(ModelUtils.getSimpleRef(refSchema.get$ref()));
				superType = translate(referencedSchema, "", null);
			}
		}

		Map<String, Schema> properties = new Hashtable<>();
		this.getAllProperties(schema, properties);
		if (getSchemaType(schema) == null && properties.isEmpty()) {