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

[Not an issue] How to create schema to generate a JSON Array using Schema and transformers provided by DataFaker API in Java? #720

Closed
amodmahajan2016 opened this issue Feb 27, 2023 · 18 comments · Fixed by #693

Comments

@amodmahajan2016
Copy link

Sorry to ask here but I am doubtful if I can get a solution on Stack overflow.

I am using DataFaker library to create schema for JSON Object and Array. Since the use of Format is deprecated, I am trying with Transformation Schemas. I tried examples given under the Format section of official documentation to create a schema for Json Array but wasn't successful. Example JSON Array -

[ { "firstName": "Oleta", "lastName": "Toy" }, { "firstName": "Gerard", "lastName": "Windler" } ]

@snuyanzin
Copy link
Collaborator

I tried examples given under the Format section of official documentation to create a schema for Json Array but wasn't successful.

could you please share the code to make the discussion more fruitful?

@erikpragt-connectid
Copy link

erikpragt-connectid commented Feb 27, 2023

Hi @amodmahajan2016 ,

I think the documentation on this is a little out of date, apologies for that. I think something like this should work:

    @Test
    fun `generate json with custom field names`() {
        val faker = Faker()

        val schema: Schema<String, *> = Schema.of(
            field("firstName", Supplier { faker.name().firstName() }),
            field("lastName", Supplier { faker.name().lastName() })
        )

        val transformer = JsonTransformer.builder<String>().formattedAs(JSON_ARRAY).build()
        val json = transformer.generate(schema, 2)

        println(json)
    }

Alternatively, in Java:

package test;

import net.datafaker.Faker;
import net.datafaker.transformations.JsonTransformer;
import net.datafaker.transformations.Schema;
import org.junit.jupiter.api.Test;

import static net.datafaker.transformations.Field.field;
import static net.datafaker.transformations.JsonTransformer.JsonTransformerBuilder.FormattedAs.JSON_ARRAY;

public class MyTest {

    @Test
    public void test() {
        var faker = new Faker();

        Schema<String, String> schema =
                Schema.of(
                        field("firstName", () -> faker.name().firstName()),
                        field("lastName", () -> faker.name().lastName()));

        JsonTransformer<String> transformer = JsonTransformer.<String>builder().formattedAs(JSON_ARRAY).build();

        String json = transformer.generate(schema, 10);

        System.out.println(json);

    }
}

Hope this helps!

@amodmahajan2016
Copy link
Author

amodmahajan2016 commented Feb 27, 2023

Thanks, it worked. But how to create JSON Array at field level then? Something as below -

[
  {
    "firstName": "Oleta",
    "lastName": "Toy",
    "skills" : [{
      "lang": "some"
    },
    {
      "lang": "boo"
    }]
  }
]

Will "Format" be removed soon as that has more clear and easy syntax?

@snuyanzin
Copy link
Collaborator

snuyanzin commented Feb 27, 2023

But how to create JSON Array at field level then

just add compositeField to schema

    compositeField("skills", new Field[] {field("lang", () -> faker.nation().language())})

full code snippet

    @Test
    void test() {
        var faker = new Faker();

        Schema<String, String> schema =
            Schema.of(
                field("firstName", () -> faker.name().firstName()),
                field("lastName", () -> faker.name().lastName()),
                compositeField("skills", new Field[] {field("lang", () -> faker.nation().language())})
            );

        JsonTransformer<String> transformer = JsonTransformer.<String>builder().formattedAs(JSON_ARRAY).build();

        String json = transformer.generate(schema, 10);

        System.out.println(json);
    }

@snuyanzin
Copy link
Collaborator

Will "Format" be removed soon

it's already removed in main if we are talking about json

@amodmahajan2016
Copy link
Author

amodmahajan2016 commented Feb 27, 2023

compositeField is generating nested JSON Object as below. My requirement is to generate a key with JSON ARRAY value.

[
  {
    "Text": "Elicia",
    "Bool": false,
    "skills": {
      "lang": "Hindi"
    }
  },
  {
    "Text": "Sheldon",
    "Bool": false,
    "skills": {
      "lang": "Arabic"
    }
  }
]

I tried as below and it generates like "ints": [1, 2, 3]. I tried putting Fields instead of Integers but it is not working.
field("ints", () -> new Object[]{1, 2, 3})

@snuyanzin
Copy link
Collaborator

ok, in this case it will be a bit different

 void test() {
        var faker = new Faker();

        Schema<String, ?> schema =
            Schema.of(
                field("firstName", () -> faker.name().firstName()),
                field("lastName", () -> faker.name().lastName()),
                field("skills", () ->
                    List.of(compositeField(null,
                        new Field[]{field("lang", () -> faker.nation().language())})
                    )
                )
            );

        JsonTransformer<String> transformer = JsonTransformer.<String>builder().formattedAs(JSON_ARRAY).build();

        String json = transformer.generate(schema, 10);

        System.out.println(json);
    }

@amodmahajan2016
Copy link
Author

amodmahajan2016 commented Feb 27, 2023

It is giving output (not correct) as below which is similar to when I used new Object[] -

{
  "firstName": "Darryl",
  "lastName": "Champlin",
  "skills": [
    "net.datafaker.transformations.CompositeField@22101c80"
  ]
}

@snuyanzin
Copy link
Collaborator

snuyanzin commented Feb 27, 2023

for me it outputs

[
{"firstName": "Tiffiny", "lastName": "Casper", "skills": [{"lang": "Zulu"}]},
{"firstName": "Lorena", "lastName": "Dibbert", "skills": [{"lang": "Japanese"}]},
{"firstName": "Jamal", "lastName": "Dibbert", "skills": [{"lang": "Russian"}]},
{"firstName": "Mario", "lastName": "Prohaska", "skills": [{"lang": "Korean"}]},
{"firstName": "Agustina", "lastName": "Pfeffer", "skills": [{"lang": "Hakka"}]},
{"firstName": "Edra", "lastName": "Watsica", "skills": [{"lang": "English"}]},
{"firstName": "Clarice", "lastName": "Ryan", "skills": [{"lang": "Marathi"}]},
{"firstName": "Edra", "lastName": "Schneider", "skills": [{"lang": "Kazakh"}]},
{"firstName": "Marlyn", "lastName": "Luettgen", "skills": [{"lang": "Japanese"}]},
{"firstName": "Art", "lastName": "Wisozk", "skills": [{"lang": "Hindi"}]}
]

are you sure you are running exactly the same code as i put in comment above?

@amodmahajan2016
Copy link
Author

amodmahajan2016 commented Feb 27, 2023

Yes, exactly the same code. I am using 1.8.0
image

@snuyanzin
Copy link
Collaborator

ok i see,

I am using 1.8.0

that is probably the reason
the example I mentioned works after merge of this #693
that is only in main and will be a part of future releases

@amodmahajan2016
Copy link
Author

ok i see,

I am using 1.8.0

that is probably the reason the example I mentioned works after merge of this #693 that is only in main and will be a part of future releases

Thanks @snuyanzin, @erikpragt-connectid. Waiting eagerly for the next release. This feature is really awesome and unfortunately many are aware of this. This will be great for API automation testing. I will post some blogs/videos on these concepts.

@kingthorin kingthorin linked a pull request Feb 27, 2023 that will close this issue
@hetzijzo
Copy link

hetzijzo commented May 5, 2023

I am using version 1.9.0 currently, but I experience the same behaviour as @amodmahajan2016. The #693 seems to be merged. What could be wrong? Has it been released yet?

@snuyanzin
Copy link
Collaborator

it's merged to 2.x (main branch)
it's not merged to 1.x branch
may be makes sense to merge to 1.x as well..

@hetzijzo
Copy link

hetzijzo commented May 5, 2023

Aha that makes sense. Also a merge into 1.x would be nice, since the 2.x is only available as SNAPSHOT version. I will make a remark in the original bug with a question if it can be merged into 1.x as well.

@bodiam
Copy link
Contributor

bodiam commented May 5, 2023

Well, 2.0 is the future release, we will release a 2.0 version soon.

@snuyanzin
Copy link
Collaborator

merged to 1.x as 8ad4e56

@snuyanzin
Copy link
Collaborator

closing since it was merged to both 1.x and 2.x
also 2.0 has been released with this fix already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants